是否容易核实某一物体属于某一类别? 例如,我可以这样做。
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
但是,这要求每次在飞行中立即发射一个新的物体,但只能予以抛弃。 是否有更好的办法检查“a”属于“MyClass”类?
是否容易核实某一物体属于某一类别? 例如,我可以这样做。
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
但是,这要求每次在飞行中立即发射一个新的物体,但只能予以抛弃。 是否有更好的办法检查“a”属于“MyClass”类?
The instanceof
keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof
will return true
for superclasses as well.
如果你想看到某一物体是某一类别的一个直接例子,你可以比较该类别。 可以通过<条码>植被(代码>)获取某一例的类别物体。 也可通过<条码>ClassName.qu自动进入特定类别。
例如:
if (a.getClass() == X.class) {
// do something
}
在上述例子中,如果<代码>a 是X
的例,而不是a
是<代码>的子类例,则该条件即为真实情况。 X 。
相比之下:
if (a instanceof X) {
// do something
}
在<条码>中 例如,如果<代码>a 是X
的例,或者a
是<>X>>的<>下级>。
大部分时间是<代码>instanceof。
如果你永远需要积极这样做,你可以采取以下措施:
boolean isInstance(Object object, Class<?> type) {
return type.isInstance(object);
}
java.lang.Class
<><>>>>>>, 点名:Class on any Object (returns thecode>Class which is an instance of), 或 您可使用字面字面字面代码(例如,Stem/>。 也有其他办法,通过反转录(http://code>Class
本身是切入点)。
使用instanceof
运营商:
if(a instanceof MyClass)
{
//do something
}
我同意使用<代码>instanceof。 前面已经提到过。
使用<代码>instanceof的另一个好处是,如果使用<编码>null,请参见instanceof
of will Return false
,而a.getClass(
>>,将 throw上
履历
通常的做法是:
if (a instanceof A)
然而,在有些情况下,如在一般论据中<代码>A。
由于 Java的类型 era缩,以下数字集:
<A> boolean someMethod(Object a) {
if (a instanceof A)
...
}
和以下的打字工作(将制作<>unchecked 警告):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
您可在操作时间投到<代码>A上,因为在操作时间,<代码>A基本上为<代码>Object。
这些案件的解决办法是使用<代码>Clas,而不是一般性论点:
void someMethod(Object a, Class<A> aClass) {
if (aClass.isInstance(a)) {
A casted = aClass.cast(a);
...
}
}
之后,你可以把这种方法称作:
someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...