Java中的instanceof及其应用

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)
  •   Java 面向对象/Java 继承

    instanceof是一个关键字,instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。

    以下是显示instanceof不同行为的Java程序。

    // Java program to demonstrate working of instanceof
    
    // Creating sample classes with parent Child
    // relationship
    class Parent {  }
    class Child extends Parent { }
    
    class Test
    {
        public static void main(String[] args)
        {
            Child cobj = new Child();
    
            // A simple case
            if (cobj instanceof Child)
               System.out.println("cobj is instance of Child");
            else
               System.out.println("cobj is NOT instance of Child");
    
            // instanceof returns true for Parent class also 
            if (cobj instanceof Parent)
               System.out.println("cobj is instance of Parent");
            else
               System.out.println("cobj is NOT instance of Parent");
    
            // instanceof returns true for all ancestors (Note : Object
            // is ancestor of all classes in Java) 
            if (cobj instanceof Object)
               System.out.println("cobj is instance of Object");
            else
               System.out.println("cobj is NOT instance of Object");           
        }
    }
    

    输出:

    cobj is instance of Child
    cobj is instance of Parent
    cobj is instance of Object

    instanceof对于null返回false

    // Java program to demonstrate that instanceof
    // returns false for null
    
    class Test {  }
    
    class Main
    {
        public static void main(String[] args)
        {
            Test tobj = null
    
            // A simple case
            if (tobj instanceof Test)
               System.out.println("tobj is instance of Test");
            else
               System.out.println("tobj is NOT instance of Test");
        }
    }
    

    输出:

    tobj is NOT instance of Test

    父对象不是Child的实例

    // A Java program to show that a parent object is
    // not an instance of Child
    
    class Parent {  }
    class Child extends Parent { }
    
    class Test
    {
        public static void main(String[] args)
        {
            Parent pobj = new Parent();
            if (pobj instanceof Child)
               System.out.println("pobj is instance of Child");
            else
               System.out.println("pobj is NOT instance of Child");
        }
    }
    

    输出:

    pobj is NOT instance of Child

    引用Child的父引用是Child的实例

    // A Java program to show that a parent reference
    // referring to a Child is an instance of Child
    
    class Parent {  }
    class Child extends Parent { }
    
    class Test
    {
        public static void main(String[] args)
        {
            // Reference is Parent type but object is
            // of child type.
            Parent cobj = new Child();
            if (cobj instanceof Child)
               System.out.println("cobj is instance of Child");
            else
               System.out.println("cobj is NOT instance of Child");
        }
    }
    

    输出:

    tobj is an instance of Child

    Java中的instanceof的应用:

    我们在这里看到,父类型数据成员在父类型的引用引用子对象时被访问。我们可以使用类型转换来访问子数据成员。语法:(child_class_name)Parent_Reference_variable).func.name()。

    当我们进行类型转换时,检查类型转换是否有效总是一个好主意。instanceof有助于在这里使用。我们总是可以首先使用instancef检查有效性,然后进行类型转换。

    // A Java program to demonstrate that non-method
    // members are accessed according to reference
    // type (Unlike methods which are accessed according
    // to the referred object)
    
    class Parent
    {
        int value = 1000;
    }
    
    class Child extends Parent
    {
        int value = 10;
    }
    
    // Driver class
    class Test
    {
        public static void main(String[] args)
        {
            Parent cobj = new Child();
            Parent par = cobj;
    
            // Using instanceof to make sure that par
            // is a valid reference before typecasting
            if (par instanceof Child)
            {
                System.out.println("Value accessed through " +
                    "parent reference with typecasting is " +
                                         ((Child)par).value);
            }
        }
    }
    

    输出:

    Value accessed through parent reference with typecasting is 10
  •   Java 面向对象/Java 继承
  •   本文标题:Java中的instanceof及其应用 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/3912.html
      微信返利机器人
      免费:淘宝,京东,拼多多优惠券
      腾讯,爱奇艺,优酷的VIP视频免费解析,免费看
      即刻扫描二维码,添加微信机器人!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注