国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中instanceof關鍵字的用法總結

2019-11-26 15:56:34
字體:
來源:轉載
供稿:網友

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  ,由于集合能夠存各種對象,所以在讀取時一般要進行相應的判斷。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀集县| 渭南市| 吉安县| 莱芜市| 广宁县| 光泽县| 桓仁| 聂荣县| 玉门市| 富阳市| 奉化市| 开远市| 衡南县| 大关县| 姚安县| 江达县| 南漳县| 江阴市| 乐亭县| 乌兰浩特市| 徐汇区| 八宿县| 育儿| 得荣县| 隆化县| 卢湾区| 鞍山市| 霞浦县| 靖江市| 建阳市| 乌拉特后旗| 广南县| 庄浪县| 夹江县| 灵宝市| 太和县| 石门县| 香格里拉县| 钟祥市| 阳谷县| 普安县|