将 instanceof 与 class 对象一起使用
Using instanceof with a class Object
实现此功能的正确语法是什么?
public boolean isTypeOf(Class type) {
return this instanceof type;
}
我打算调用它:
foo.isTypeOf(MyClass.class);
该方法将被覆盖,否则我将直接使用 instanceof
。
public boolean isTypeOf(Class type) {
return type.isInstance(this);
}
此方法确定给定参数是否为 class 的实例。如果对象是 class.
的子 class,则此方法也有效
引用自 Javadoc:
This method is the dynamic equivalent of the Java language instanceof
operator.
实现此功能的正确语法是什么?
public boolean isTypeOf(Class type) {
return this instanceof type;
}
我打算调用它:
foo.isTypeOf(MyClass.class);
该方法将被覆盖,否则我将直接使用 instanceof
。
public boolean isTypeOf(Class type) {
return type.isInstance(this);
}
此方法确定给定参数是否为 class 的实例。如果对象是 class.
的子 class,则此方法也有效引用自 Javadoc:
This method is the dynamic equivalent of the Java language
instanceof
operator.