我为这个简单的问题搜索了 30 多分钟,但找不到任何有用的信息。
我想写一个接受类作为参数的方法。 此外,我必须保证该类不是任何类型的类,而是一个接口(interface)(例如 Action.class、Collection.class...)。
所以它必须看起来类似于下面的行。但是我需要在 HELP 中指定什么地方呢?
public void registerInterface(Class<HELP> anInterfaceClass) {
// do stuff with anInterfaceClass
}
我无法放入对象。也许类 :-) 很简单,但我找不到解决方案....
最佳答案
我认为没有编译时检查泛型参数是否为接口(interface)。不过,您可以进行运行时检查。
public void registerInterface(Class<?> anInterfaceClass) {
if (!anInterfaceClass.isInterface()) {
throw new IllegalArgumentException("An interface is expected!");
}
关于java - 所有接口(interface)的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110833/