qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          java中instanceof的用法

             java 中的instanceof 運算符是用來在運行時指出對象是否是特定類的一個實例。instanceof通過返回一個布爾值來指出,這個對象是否是這個特定類或者是它的子類的一個實例。
           用法:
          result = object instanceof class
          參數(shù):
          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

          posted on 2012-11-21 22:47 順其自然EVO 閱讀(201) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2012年11月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 攀枝花市| 元江| 高要市| 临潭县| 陇西县| 姚安县| 长乐市| 筠连县| 聊城市| 泾源县| 苏尼特右旗| 遵义市| 清徐县| 铁岭县| 射阳县| 镇平县| 永川市| 望奎县| 通道| 秦安县| 昆明市| 莱西市| 萍乡市| 彰化县| 安康市| 荥阳市| 财经| 上饶县| 渭南市| 北票市| 玉溪市| 南京市| 彩票| 涡阳县| 绥阳县| 奇台县| 文水县| 余江县| 柳江县| 宁强县| 革吉县|