Java中instanceof關鍵字的用法總結
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("\ninstanceoftest 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("\ninstanceoftest 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 ,由於集合能夠存各種對象,所以在讀取時一般要進行相應的判斷。
Java中instanceof關鍵字的用法總結