zoukankan      html  css  js  c++  java
  • Java中instanceof关键字的用法总结

    instanceof是Java的一个二元操作符,和==,>,<是同一类东东。由于它是由字母组成的,所以也是Java的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据

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

    用法:
    result = object instanceof class
    参数:
    Result:布尔类型。
    Object:必选项。任意对象表达式。
    Class:必选项。任意已定义的对象类。
    说明:
    如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。

    例子如下:

    复制代码 代码如下:

    package com.instanceoftest;
    interface A{}
    class B implements A{

    }
    class C extends B {

    }

    class instanceoftest {
    public static void main(String[] args){
    A a=null;
    B b=null;
    boolean res;

    System.out.println("instanceoftest test case 1: ------------------");
    res = a instanceof A;
    System.out.println("a instanceof A: " + res);

    res = b instanceof B;
    System.out.println("b instanceof B: " + res);

    System.out.println(" instanceoftest test case 2: ------------------");
    a=new B();
    b=new B();

    res = a instanceof A;
    System.out.println("a instanceof A: " + res);

    res = a instanceof B;
    System.out.println("a instanceof B: " + res);
    res = b instanceof A;
    System.out.println("b instanceof A: " + res);

    res = b instanceof B;
    System.out.println("b instanceof B: " + res);

    System.out.println(" instanceoftest test case 3: ------------------");
    B b2=(C)new C();

    res = b2 instanceof A;
    System.out.println("b2 instanceof A: " + res);

    res = b2 instanceof B;
    System.out.println("b2 instanceof B: " + res);

    res = b2 instanceof C;
    System.out.println("b2 instanceof C: " + res);
    }
    }

    /*
    result:

    instanceoftest test case 1: ------------------
    a instanceof A: false
    b instanceof B: false
    instanceoftest test case 2: ------------------
    a instanceof A: true
    a instanceof B: true
    b instanceof A: true
    b instanceof B: true
    instanceoftest test case 3: ------------------
    b2 instanceof A: true
    b2 instanceof B: true
    b2 instanceof C: true

    instanceof 通常用于根据不同的实例调用不同的方法:

    一、在有继承关系的类中我们可以通过多态来调用不同实例中的不同方法:

    例1:

      有三个类,类名以及它们之间的关系如下

       Animal (Superclass)     Dog(Subclass)     Cat(Subclass)

       则可得出如下对象

       Animal  animal =new Animal (); ====》animal  instanceof Animal    返回 true

       Dog   dog=new  Dog();====》dog  instanceof  Dog    返回 true

       Cat    cat=new  Cat();====》cat  instanceof  Cat    返回  true

       Animal  dog=new  Dog();====》dog  instanceof  Animal    返回 true

       Animal  cat=new  Cat();====》cat  instanceof  Animal    返回 true

     1  
     2 Animal dog=new Dog();
     3   Animal cat=new Cat();
     4 
     5   List list = new ArrayList();
     6 
     7   list.add(dog);
     8   list.add(cat);
     9 
    10   Iterator it = list.iterator();
    11   while (it.hasNext()) {
    12      it.next().animalDo();
    13 
    14   }

    在这里我们可以在Dog与Cat类中重写Animal中的animalDo方法,通过调用animalDo方法,然后会自动根据不同的实例调用不同类中的方法.

    二、在没有继承关系的类中,我们可以通过instanceof来判断当前实例,然后很据不同实例调用不同方法:

    例2: 


     1   Station s = new Station();
     2   Cell c = new Cell();
     3 
     4 
     5   List list = new ArrayList();
     6 
     7   list.add(s);
     8   list.add(c);
     9 
    10 
    11   Iterator it = list.iterator();
    12   while (it.hasNext()) {
    13    Object obj = it.next();
    14    if (obj instanceof Station ) {
    15     Station s1 = (Station ) obj;
    16     s1.stationDo();
    17    }
    18    if (obj instanceof Cell ) {
    19     Cell c1 = (Cell ) obj;
    20     c1.cellDo();
    21    }
    22   }

     在这里我们可以通过instanceof 判断结果,执行不同类中的相应动作方法(stationDo()、cellDo())。

    一般在使用无泛型的集合(List、set等)时,比较多的使用  instanceof  ,由于集合能够存各种对象,所以在读取时一般要进行相应的判断。

  • 相关阅读:
    NDK中使用pthread多线程中自己写的一个BUG
    Android Native crash日志分析
    Android细笔记--DataStorage
    求二叉树第n层节点数
    对YUV数据进行裁剪
    Android XML中引用自定义内部类view的四个why
    Android细笔记--ContentProvider
    Android Log Tag含义
    189-RotaeArray
    二分查找法
  • 原文地址:https://www.cnblogs.com/the-wang/p/7271843.html
Copyright © 2011-2022 走看看