java - 如何检查我的对象是否属于给定类的类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25961327/
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
java - How do I check if my object is of type of a given class?
提问by Bick
My method gets Class as a parameter and I have to check that one my variables is of type class.
我的方法将 Class 作为参数,我必须检查我的变量是否为 class 类型。
Volvo v1 = new Volvo();
Class aClass = v1.getClass();
check(aClass);
inside I need to do something like
在里面我需要做类似的事情
v2 instanceof aClass ? "True" : "False");
but this doesn;t compile .
但这不会编译。
采纳答案by markspace
I think you want aClass.isInstance( v2 )
. The docs say it works the same as the instanceOf
keyword. I guess they can't use instanceOf
as a method name because keywords can't be used as method names.
我想你想要aClass.isInstance( v2 )
。该文件说它的工作原理相同instanceOf
的关键字。我猜它们不能instanceOf
用作方法名称,因为关键字不能用作方法名称。
v2 = ???
Volvo v1 = new Volvo();
Class aClass = v1.getClass();
aClass.isInstance( v2 ) // "check(aClass)"
Or maybe just use a class literal, if "Volvo" is a constant.
或者也许只是使用类文字,如果“沃尔沃”是一个常量。
v2 = ???
Volvo.class.isInstance( v2 );
回答by cjcdoomed
Volvo v = new Volvo();
if (v instanceof Volvo) {
System.out.println("I'm boxy, but safe.");
}
回答by frankfg
You can try also:
你也可以试试:
aClass.isAssignableFrom(v2.getClass())
Where "aClass" is a superclass of "v2"
其中“aClass”是“v2”的超类
回答by user268396
As Hovercraft Full Of Eels mentioned, you can generally avoid such explicit method calls by leaning on the compiler to do the checking for you. Some approaches:
正如 Hovercraft Full Of Eels 所提到的,您通常可以通过依靠编译器为您进行检查来避免此类显式方法调用。一些方法:
You are writing a container of a generically typed thing. Let the type of thing be
T
, then Java allows you to express this as follows:public class MyClass<T> { private T thing; public void setThing(T t) { this.thing = t; } private void check(Class<T> cls) {} // probably not necessary }
Then create instances of
MyClass<T>
as follows:MyClass<Volvo> m = new MyClass<Volvo>();
or since Java 7:MyClass<Volvo> m = new MyClass<>();
You really do need the Class, say because you are working with Enum types and a lot of those methods a parametrized using the relevant Class:
public void check(Class<?extends SuperType> validClsAutomaticallyChecked) {}
You just need an object of a specific supertype to be passed to the method:
public void pass(SuperType thatSimple) {}
You do not need, cannot or will not make your containing class take a type parameter of the thing to be passed, but you still want to leverage generics in your method. This may be useful to allow the compiler to infer that what you are doing inside the method body is safe. For example you may wish to create a collection of things of type
T
based on the type of thing passed as parameter to the method.public <T> void workWith(T genericallyTypedObject) {}
您正在编写一个泛型类型的容器。假设 thing 的类型为
T
,那么 Java 允许您将其表达如下:public class MyClass<T> { private T thing; public void setThing(T t) { this.thing = t; } private void check(Class<T> cls) {} // probably not necessary }
然后创建
MyClass<T>
如下实例:MyClass<Volvo> m = new MyClass<Volvo>();
或自 Java 7:MyClass<Volvo> m = new MyClass<>();
您确实需要 Class,因为您正在使用 Enum 类型和许多使用相关 Class 参数化的方法:
public void check(Class<?extends SuperType> validClsAutomaticallyChecked) {}
您只需要将特定超类型的对象传递给该方法:
public void pass(SuperType thatSimple) {}
您不需要、不能或不会让您的包含类采用要传递的事物的类型参数,但您仍然希望在您的方法中利用泛型。这可能有助于编译器推断您在方法体内所做的事情是安全的。例如,您可能希望
T
基于作为参数传递给方法的事物类型来创建类型的事物集合。public <T> void workWith(T genericallyTypedObject) {}