public class TableModel2 extends TableModel1 { ... }
TableModel2 tableModel = new TableModel2();
boolean t1 = tableModel instanceof TableModel1;
boolean t2 = tableModel instanceof TableModel2;
In the above example, t1
and t2
are true
. So, how could I differentiate between TableModel1
and TableModel2
using instanceof
?
boolean t2 = tableModel.getClass().equals(TableModel1.class); //False
boolean t2 = tableModel.getClass().equals(TableModel2.class); //True
You cannot do it with instanceof
, but you can do it with getClass
:
boolean t1 = tableModel.getClass().equals(TableModel1.class);
boolean t2 = tableModel.getClass().equals(TableModel2.class);
The instanceof
operator is intended for checking class hierarchy all the way, down to the java.lang.Object
, including the checks for all interfaces. It lets you know if an instance of the object you have can be cast to the type you specified without triggering class cast exception.
getClass
, on the other hand, returns the specific class of the given object.
So, how could I differentiate between TableModel1 and TableModel2 using instanceof?
Technically, you can check that tableModel
is an instance of TableModel1
and not and instance of TableModel2
:
(tableModel instanceof TableModel1) && !(tableModel instanceof TableModel2)
However, I would like to encourage you in the strongest possible terms to avoid any code that branches based on the result of instanceof
or getClass()
. Such code is very fragile in the face of future changes. If you find yourself doing anything along these lines, it's a strong clue that it may be a good time to revisit your design.
instance of means "is a".
TableModel2 IS A TableModel1.
But TableModel1 IS NOT A TableModel2.
so
package main;
public class TempClass {
public static void main(String[] args) {
TableModel1 tableModel1 = new TableModel1();
TableModel1 tableModel2 = new TableModel2();
System.out.println(tableModel1 instanceof TableModel1);
System.out.println(tableModel1 instanceof TableModel2);
System.out.println(tableModel2 instanceof TableModel1);
System.out.println(tableModel2 instanceof TableModel2);
}
public static class TableModel1 {
}
public static class TableModel2 extends TableModel1 {
}
}
true
false
true
true
You cannot. If you really need to tell one from another, use tableModel.getClass()
instead, as in:
boolean t1 = tableModel.getClass() == TableModel1.class;
boolean t2 = tableModel.getClass() == TableModel2.class;
Note though you're deliberately trying to break one of founding principles of OOP, so make sure you cannot avoid this trick before you use it in a real code.
Well, I don't believe you can. From inheritance perspective tableModel
is perfectly valid reference for both types, so instanceof
will return true in both cases.
Easy, use class names:
public class Test
{
public class TableModel1
{
};
public class TableModel2 extends TableModel1
{
};
public TableModel2 tableModel = new TableModel2();
public static void main(String[] args)
{
Test t = new Test();
System.out.println("t1=" + t.tableModel.getClass().equals(TableModel1.class));
System.out.println("t2=" + t.tableModel.getClass().equals(TableModel2.class));
}
}
来源:https://stackoverflow.com/questions/9174966/example-of-%c2%b4instanceof%c2%b4