• Class instanceof isInstance


    class Base{}
    class Derived extends Base{}
    class HiDerived extends Derived{}
    /**
     * 2018/09/11 <<thinking in java>> 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

  • 相关阅读:
    day03--变量与基本数据类型
    day02--编程语言的分类与Python开发环境的搭建
    day01--编程与计算机组成原理
    基本数据类型操作
    python格式化字符串
    Python垃圾回收机制
    day04作业
    day03作业
    Pycharm2018安装与激活
    Python入门-python浅谈
  • 原文地址:https://www.cnblogs.com/--zz/p/9630415.html
Copyright © 2020-2023  润新知