java 如何使用反射比较类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/825654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compare classes using reflection?
提问by kgrad
I am trying to determine the class type of a class using reflection and then do something specific. For example, if the class is a double, use a double specific method.
我正在尝试使用反射来确定类的类类型,然后执行一些特定的操作。例如,如果类是双精度型,则使用双精度型方法。
I am attempting to use
我正在尝试使用
if(f.getClass() == Double.class)
However, I am getting a compiler error:
但是,我收到编译器错误:
"Incompatible operand types Class <capture#1-of ? extends Field> and Class<Double>"
“不兼容的操作数类型 Class <capture#1-of ? extends Field> 和 Class<Double>”
What is the proper way to do this?
这样做的正确方法是什么?
Edit: to be more clear
编辑:更清楚
f is of type Field. obtained by reflection in a loop
f 是字段类型。通过循环反射获得
(Field f : instance.getDeclaredFields())
采纳答案by StaxMan
Interesting error message (I didn't know '==' operator would check those). But based on it, I suspect that your comparison is wrong: you are trying to see if Field class (theoretically its super-class, but only in theory -- Field is final) is same as Double.class, which it can not be.
有趣的错误消息(我不知道 '==' 运算符会检查这些)。但是基于它,我怀疑您的比较是错误的:您正在尝试查看 Field 类(理论上是它的超类,但仅在理论上 - Field 是最终的)是否与 Double.class 相同,它不能.
So: yes, comparison should work, iff you give it right arguments. So I suspect you want to do:
所以:是的,比较应该有效,如果你给出正确的论据。所以我怀疑你想做:
if (f.getType() == Double.class)
if (f.getType() == Double.class)
instead. And that should work, given that Double is a final class. Otherwise "isAssignableFrom" would be more appropriate.
反而。考虑到 Double 是最后一堂课,这应该可行。否则“isAssignableFrom”会更合适。
回答by Roman
If you have objects then use
如果您有对象,则使用
if (f instanceof Double) { }
Another interesting thing is method isAssignableFrom:
另一个有趣的事情是方法 isAssignableFrom:
if (f.getClass().isAssignableFrom (Double.class)) { }
But in general it's a bad style. Use polymorphism to implement logic which depends on class types.
但总的来说,这是一种糟糕的风格。使用多态来实现依赖于类类型的逻辑。
Answer for comment: f instanceof Double works fine.
回答评论: f instanceof Double 工作正常。
You probably wrote something like this:
你可能写了这样的东西:
float f = 1.1f;
if (f instanceof Double) { ..}
And smart java compiler says that you've got CE. BUT:
智能 Java 编译器会说您已经获得了 CE。但:
public static boolean isInstanceOfDouble (Object obj) {
return obj instanceof Double;
}
psvm (String [] args) {
sout (isInstanceOfDouble (1.1f);
}
..this works fine
..这很好用
回答by kgrad
You should be using getType() to get the underlying type, not get class. getType() returns the class of the underlying type as required.
您应该使用 getType() 来获取底层类型,而不是获取类。getType() 根据需要返回底层类型的类。
the code looks like this:
代码如下所示:
if(f.getType().equals(Double.class))
回答by Michael Myers
The simplest way would be
最简单的方法是
if (f.getClass().equals(Double.class))
Edit:Ah, now I see the problem. The error you were getting was because (as a result of generics) the compiler could tell that the classes would never be equal (Field.classcannot equal Double.class). Using .equals() turns it into a warning instead of an error, but it's still wrong code.
编辑:啊,现在我看到了问题。你得到的错误是因为(作为泛型的结果)编译器可以告诉类永远不会相等(Field.class不能相等Double.class)。使用 .equals() 会将它变成警告而不是错误,但它仍然是错误的代码。
Field.getType()gives you the type of the field. Since this cannot be known at compile time, you will not get an error if you use the ==operator as you did originally.
Field.getType()给你字段的类型。由于在编译时无法知道这一点,因此如果您==像最初那样使用该运算符,则不会出现错误。
回答by Jiang Run
Following code snippet should work perfectly:
以下代码片段应该可以完美运行:
if (f.getType() == Double.TYPE)
BTW, neither ==nor equalsworks for Numbertypes according to my test (JRE7)
顺便说一句,根据我的测试(JRE7),既不适用于数字类型,==也不equals适用于数字类型

