• Java中instanceof和isInstance区别详解


     一次性搞定instanceof和isInstance,instanceof和isInstance长的非常像,用法也很类似,先看看这两个的用法:

    obj.instanceof(class)

    也就是说这个对象是不是这种类型,

    1.一个对象是本身类的一个对象

    2.一个对象是本身类父类(父类的父类)和接口(接口的接口)的一个对象

    3.所有对象都是Object

    4.凡是null有关的都是false  null.instanceof(class)

    class.inInstance(obj)

    这个对象能不能被转化为这个类

    1.一个对象是本身类的一个对象

    2.一个对象能被转化为本身类所继承类(父类的父类等)和实现的接口(接口的父接口)强转

    3.所有对象都能被Object的强转

    4.凡是null有关的都是false   class.inInstance(null)

    类名.class和对象.getClass()几乎没有区别,因为一个类被类加载器加载后,就是唯一的一个类。

    一个实例搞定:

    class A {
    }
    
    class B extends A {
    }
    
    public class Test {
        public static void main(String[] args) {
    
            B b = new B();
            A a = new A();
            A ba = new B();
            System.out.println("1------------");
            System.out.println(b instanceof B);
            System.out.println(b instanceof A);
            System.out.println(b instanceof Object);
            System.out.println(null instanceof Object);
            System.out.println("2------------");
            System.out.println(b.getClass().isInstance(b));
            System.out.println(b.getClass().isInstance(a));
            System.out.println("3------------");
            System.out.println(a.getClass().isInstance(ba));
            System.out.println(b.getClass().isInstance(ba));
            System.out.println(b.getClass().isInstance(null));
            System.out.println("4------------");
            System.out.println(A.class.isInstance(a));
            System.out.println(A.class.isInstance(b));
            System.out.println(A.class.isInstance(ba));
            System.out.println("5------------");
            System.out.println(B.class.isInstance(a));
            System.out.println(B.class.isInstance(b));
            System.out.println(B.class.isInstance(ba));
            System.out.println("6------------");
            System.out.println(Object.class.isInstance(b));
            System.out.println(Object.class.isInstance(null));
        }
    }

    运行结果:

  • 相关阅读:
    What is OpenOCD?
    OpenOCD Debug Adapter Configuration
    OpenOCD 0.9.0 release
    ARM DEBUGGER FOR NEARLY ONE DOLLAR
    SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic
    SWD Connect/Transfer Source Code
    Dapper Miser implementation of CMSIS-DAP, MC HCK as SWD Adapter
    STM32定时器级联 -- AN2592
    Training JTAG Interface
    JTAG Simplified
  • 原文地址:https://www.cnblogs.com/greatfish/p/6096038.html
Copyright © 2020-2023  润新知