博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Class instanceof isInstance
阅读量:6827 次
发布时间:2019-06-26

本文共 2563 字,大约阅读时间需要 8 分钟。

class Base{}class Derived extends Base{}class HiDerived extends Derived{}/** * 2018/09/11 <
> RTTI * instanceof Class isInstance 等价性 * @author zz2108828 */public class Test{ static void test (Object x) { System.out.println("-----------Current type: " + x.getClass()); System.out.println("x instance of Base " + (x instanceof Base)); System.out.println("x instance of Derived " + (x instanceof Derived)); System.out.println("x instance of HiDerived " + (x instanceof HiDerived)); System.out.println("Base.isInstance(x) " + Base.class.isInstance(x)); System.out.println("Derived.isInstance(x) " + Derived.class.isInstance(x)); System.out.println("HiDerived.isInstance(x) " + HiDerived.class.isInstance(x)); System.out.println("x.getClass() == Base.class "+ (x.getClass() == Base.class)); System.out.println("x.getClass() == Derived.class " + (x.getClass() == Derived.class)); System.out.println("x.getClass().equals(Base.class) " + x.getClass().equals(Base.class)); System.out.println("x.getClass().equals(Derived.class) "+x.getClass().equals(Derived.class)); System.out.println("x.getClass().isAssignableFrom(Base.class) " +(x.getClass().isAssignableFrom(Base.class))); System.out.println("x.getClass().isAssignableFrom(Derived.class) " +(x.getClass().isAssignableFrom(Derived.class))); System.out.println("x.getClass().isAssignableFrom(HiDerived.class) " +(x.getClass().isAssignableFrom(HiDerived.class))); } public static void main(String[] args) { test(new Base()); test(new Derived()); }}

-----------Current type: class Base

x instance of Base true
x instance of Derived false
x instance of HiDerived false
Base.isInstance(x) true
Derived.isInstance(x) false
HiDerived.isInstance(x) false
x.getClass() == Base.class true
x.getClass() == Derived.class false
x.getClass().equals(Base.class) true
x.getClass().equals(Derived.class) false
x.getClass().isAssignableFrom(Base.class) true
x.getClass().isAssignableFrom(Derived.class) true
x.getClass().isAssignableFrom(HiDerived.class) true
-----------Current type: class Derived
x instance of Base true
x instance of Derived true
x instance of HiDerived false
Base.isInstance(x) true
Derived.isInstance(x) true
HiDerived.isInstance(x) false
x.getClass() == Base.class false
x.getClass() == Derived.class true
x.getClass().equals(Base.class) false
x.getClass().equals(Derived.class) true
x.getClass().isAssignableFrom(Base.class) false
x.getClass().isAssignableFrom(Derived.class) true
x.getClass().isAssignableFrom(HiDerived.class) true

转载于:https://www.cnblogs.com/--zz/p/9630415.html

你可能感兴趣的文章
Java9新特性系列(Stream改进)
查看>>
Android输入系统(四)输入事件是如何分发到Window的?
查看>>
Docker基本概念详解
查看>>
Mybatis第四篇【多表连接】
查看>>
看完这篇,你就会明白我为什么要学python了
查看>>
React+ Redux + React-route + Axios 实战,很适合进阶
查看>>
React Router 4 简介及其背后的路由哲学
查看>>
面向对象 - Java那些事儿
查看>>
ObjC中的TypeEncodings
查看>>
干货满满,腾讯云+社区技术沙龙 Kafka Meetup 深圳站圆满结束
查看>>
利用 TensorFlow 入门 Word2Vec
查看>>
组成 TensorFlow 核心的六篇论文
查看>>
从django的SECRET_KEY到代码执行
查看>>
一个轮子搞定 Fragment 和状态栏那些事
查看>>
leetcode 686. Repeated String Match 题解
查看>>
java 操作符详解
查看>>
SpringBoot整合Dubbo2.5.10
查看>>
【ES6基础】const介绍
查看>>
使用Java Socket手撸一个http服务器
查看>>
node-sass安装失败的究极解决方法与简单使用
查看>>