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是Java的一個二元操作符,和==,>,<是同一類東東。由於它是由字母組成的,所以也是Java的保留關鍵字。它的作用是測試它左邊的對象是否是它右邊的類的實例,返回boolean類型的數據。

用法: 某個實例對象      instanceof      某個類名

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

例子如下:

 Animal dog=new Dog();
  Animal cat=new Cat();
  List list = new ArrayList();
  list.add(dog);
  list.add(cat);
  Iterator it = list.iterator();
  while (it.hasNext()) {
     it.next().animalDo();
  }

在這裏我們可以在Dog與Cat類中重寫Animal中的animalDo方法,通過調用animalDo方法,然後會自動根據不同的實例調用不同類中的方法.

二、在沒有繼承關係的類中,我們可以通過instanceof來判斷當前實例,然後很據不同實例調用不同方法:

例2:  

Station s = new Station();
  Cell c = new Cell();

  List list = new ArrayList();
  list.add(s);
  list.add(c);

  Iterator it = list.iterator();
  while (it.hasNext()) {
   Object obj = it.next();
   if (obj instanceof Station ) {
    Station s1 = (Station ) obj;
    s1.stationDo();
   }
   if (obj instanceof Cell ) {
    Cell c1 = (Cell ) obj;
    c1.cellDo();
   }
  }

在這裏我們可以通過instanceof 判斷結果,執行不同類中的相應動作方法(stationDo()、cellDo())。

一般在使用無泛型的集合(List、set等)時,比較多的使用  instanceof  ,由於集合能夠存各種對象,所以在讀取時一般要進行相應的判斷。


轉自:http://www.jb51.net/article/42276.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章