在Java中,我定义了一个接口(interface)MyInterfaceName [MyInterfaceName.class]。该 .class 文件在 jarFile 中可用。
使用 Java Reflection,我尝试在这个特定类中使用方法 class.isInterface()
,但它返回 false。
再次,基于以下引用文献,
http://tutorials.jenkov.com/java-reflection/classes.html#modifiers http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Modifier.html#isInterface(int)
我尝试了以下代码:
int modifierValue = MyInterfaceName.class.getModifiers();
System.out.println(modifierValue)
boolean interfaceFound = Modifier.isInterface(modifierValue);
System.out.println(interfaceFound)
此interfaceFound
仅返回false
。此外,modifierValue
返回 17
,它不适合 ModifierValueLink 中的任何位置。我已经提供了。
但是MyInterfaceName是根据java标准定义的接口(interface)。 我不明白为什么它不起作用。
请帮忙。谢谢。
最佳答案
我的新答案在此链接上。 http://java2s.com/Tutorials/Java/java.lang.reflect/Modifier/Java_Modifier_isInterface_int_mod_.htm
我已经在链接中实现了给定的示例;
public interface MyInterfaceName {
public int a = 0;
public int b = 0;
}
import java.lang.reflect.Modifier;
public class Main {
public static void main(String[] args) {
Class<?> cls = MyInterfaceName.class;
int modifier = cls.getModifiers();
System.out.println(Modifier.isInterface(modifier));
}
}
这给出了正确的结果。正确的用法应该如链接所示。
关于Java isInterface() 不会为接口(interface)返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562832/