检查类型是否是接口

时间:2023-01-12 17:01:21

I want to validate a parameter sent to a method, it must be an interface type. What to ask?

我想验证发送给方法的参数,它必须是一个接口类型。问什么?

void (Class<I> interfaceType){
  if (thisisnotaninterface){
    throw...
  }
}

2 个解决方案

#1


9  

Just use Class#isInterface() to check that

使用Class#isInterface()检查一下

And seriously, you should be reading the Javadocs before asking here.

认真地说,在这里提问之前,您应该先阅读Javadocs。

#2


15  

You have got a Class#isInterface() method that does exactly what you want: -

您有一个类#isInterface()方法,它完全按照您的要求:-

if (!interfaceType.isInterface()) {
    throw...
}

#1


9  

Just use Class#isInterface() to check that

使用Class#isInterface()检查一下

And seriously, you should be reading the Javadocs before asking here.

认真地说,在这里提问之前,您应该先阅读Javadocs。

#2


15  

You have got a Class#isInterface() method that does exactly what you want: -

您有一个类#isInterface()方法,它完全按照您的要求:-

if (!interfaceType.isInterface()) {
    throw...
}