将 instanceof 缩小到特定的子类
narrow down instanceof to a specific subclass
如何缩小 instanceof
到特定的 child 而不会以 parent class 结束。
例如,如果我应该有一个 parent class WhosebugUsers
那么我有 TenKUsers
、HundredKusers
等的直接子 class等等..现在我的问题是我如何使用instanceof
来获取Object
是否是WhosebugUsers
的直接子class之一的实例并且没有结束可以选择我的 object
是 WhosebugUsers
的一个实例 .. 这可能吗?
只需使用 instanceof TenKUsers
(等)成语。
它会告诉您您正在查看的对象是否是该基数的子对象 class。
由于您暗示 TenKUsers
是 WhosebugUsers
的子对象,因此 instanceof WhosebugUsers
将 也 成为该对象的 true
,当然。
您可以通过检查 .getClass()
排除超类本身,但 "grandchildren" 也将被匹配...
您可以使用反射,但通常应避免反射(有点 "cheating")。
public class T {
static class WhosebugUsers {}
static class TenKUsers extends WhosebugUsers {}
static class HundredKusers extends WhosebugUsers {}
static class EtcEtc extends TenKUsers {}
public static void main(String[] args) {
System.out.println("Instance of String: "+checkInstance(new String()));
System.out.println("Instance of WhosebugUsers (superclass): "+checkInstance(new WhosebugUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstance(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstance(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstance(new EtcEtc()));
System.out.println();
System.out.println("using reflection:");
System.out.println("Instance of String: "+checkInstanceUsingReflection(new String()));
System.out.println("Instance of WhosebugUsers (superclass): "+checkInstanceUsingReflection(new WhosebugUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstanceUsingReflection(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstanceUsingReflection(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstanceUsingReflection(new EtcEtc()));
}
private static boolean checkInstance(Object o) {
return (o instanceof WhosebugUsers) && (o.getClass() != WhosebugUsers.class);
}
private static boolean checkInstanceUsingReflection(Object o) {
Class<?> superclass = o.getClass().getSuperclass();
return superclass == WhosebugUsers.class;
}
}
哪个returns:
Instance of String: false
Instance of WhosebugUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): true
using reflection:
Instance of String: false
Instance of WhosebugUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): false
也许你应该重新考虑你的 类。似乎 TenKUsers
也可能是存储在 WhosebugUsers 中的布尔标志。甚至更好:给你的 WhosebugUsers 一个 EnumSet<Archievments>
并使 Archievments 包含 "TenKUser" 和 "HundredKusers".
除了 Mena 的回答,如果你想使用 instanceof
来检查,你可以让你的超类 Whosebugusers
抽象以确保没有实例可以存在然后检查:
object instanceof Whosebugusers
如何缩小 instanceof
到特定的 child 而不会以 parent class 结束。
例如,如果我应该有一个 parent class WhosebugUsers
那么我有 TenKUsers
、HundredKusers
等的直接子 class等等..现在我的问题是我如何使用instanceof
来获取Object
是否是WhosebugUsers
的直接子class之一的实例并且没有结束可以选择我的 object
是 WhosebugUsers
的一个实例 .. 这可能吗?
只需使用 instanceof TenKUsers
(等)成语。
它会告诉您您正在查看的对象是否是该基数的子对象 class。
由于您暗示 TenKUsers
是 WhosebugUsers
的子对象,因此 instanceof WhosebugUsers
将 也 成为该对象的 true
,当然。
您可以通过检查 .getClass()
排除超类本身,但 "grandchildren" 也将被匹配...
您可以使用反射,但通常应避免反射(有点 "cheating")。
public class T {
static class WhosebugUsers {}
static class TenKUsers extends WhosebugUsers {}
static class HundredKusers extends WhosebugUsers {}
static class EtcEtc extends TenKUsers {}
public static void main(String[] args) {
System.out.println("Instance of String: "+checkInstance(new String()));
System.out.println("Instance of WhosebugUsers (superclass): "+checkInstance(new WhosebugUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstance(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstance(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstance(new EtcEtc()));
System.out.println();
System.out.println("using reflection:");
System.out.println("Instance of String: "+checkInstanceUsingReflection(new String()));
System.out.println("Instance of WhosebugUsers (superclass): "+checkInstanceUsingReflection(new WhosebugUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstanceUsingReflection(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstanceUsingReflection(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstanceUsingReflection(new EtcEtc()));
}
private static boolean checkInstance(Object o) {
return (o instanceof WhosebugUsers) && (o.getClass() != WhosebugUsers.class);
}
private static boolean checkInstanceUsingReflection(Object o) {
Class<?> superclass = o.getClass().getSuperclass();
return superclass == WhosebugUsers.class;
}
}
哪个returns:
Instance of String: false
Instance of WhosebugUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): true
using reflection:
Instance of String: false
Instance of WhosebugUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): false
也许你应该重新考虑你的 类。似乎 TenKUsers
也可能是存储在 WhosebugUsers 中的布尔标志。甚至更好:给你的 WhosebugUsers 一个 EnumSet<Archievments>
并使 Archievments 包含 "TenKUser" 和 "HundredKusers".
除了 Mena 的回答,如果你想使用 instanceof
来检查,你可以让你的超类 Whosebugusers
抽象以确保没有实例可以存在然后检查:
object instanceof Whosebugusers