Example of ´instanceof´

一世执手 提交于 2019-11-30 04:46:54
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));
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!