'instanceof'的例子 [英] Example of ´instanceof´

查看:91
本文介绍了'instanceof'的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class TableModel2 extends TableModel1 { ... }

TableModel2 tableModel = new TableModel2();

boolean t1 = tableModel instanceof TableModel1;
boolean t2 = tableModel instanceof TableModel2;

在上面的例子中, t1 t2 true 。那么,我怎么能区分 TableModel1 TableModel2 使用 instanceof

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

这篇关于'instanceof'的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆