BlogJava 聯(lián)系 聚合 管理  

          Blog Stats

          News

           

          藍(lán)冰飛寒個(gè)人獨(dú)立博客地址,www.uphenan.com

          隨筆檔案

          文章檔案


          藍(lán)冰飛寒

          用心去做每一件事情

          instanceof  instanceof是Java的一個(gè)二元操作符,和==,>,<是同一類(lèi)東西。由于它是由字母組成的,所以也是Java的保留關(guān)鍵字。它的作用是測(cè)試它左邊的對(duì)象是否是它右邊的類(lèi)的實(shí)例,返回boolean類(lèi)型的數(shù)據(jù)。舉個(gè)例子:
            String s = "I AM an Object!";
            boolean isObject = s instanceof Object;

          給出個(gè)例題: class Animal{
              public String name ;
              public Animal(String name){
                  this.name=name;
              }
          }
          class Dog extends Animal{
              public String furcolor;
              public Dog(String n,String furcolor){
                      super(n);this.furcolor=furcolor;
              }
          }
          class Cat extends Animal{
              public String eyecolor;
              public Cat(String n,String eyecolor){
                  super(n);this.eyecolor=eyecolor;
              }
          }



          public class Test{
                public static void main(String args[]){
                    Animal a= new Animal("name");
                    Cat cat=new Cat("xiaohua","blue");
                    Dog dog=new Dog("xiaohuang","Yellow");
                    
                    System.out.println(a instanceof Animal);
                    System.out.println(cat instanceof Animal);
                    System.out.println(dog instanceof Animal);
                    System.out.println(a instanceof Cat);
                      a=new Dog("BigYellow","Yellow");
                      System.out.println(a.name);
                      Dog b=(Dog)a;
                      System.out.println(b.furcolor);
                }
          }
          Tags -
          文章來(lái)源:http://www.tt-shopping.com/kevinlau/read.php/110.htm
          posted on 2009-05-09 21:59 藍(lán)冰飛寒 閱讀(71) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 冷水江市| 达拉特旗| 夏河县| 任丘市| 崇文区| 贵定县| 浦县| 油尖旺区| 比如县| 安龙县| 时尚| 东城区| 托克托县| 安庆市| 黄山市| 孝昌县| 庆阳市| 梓潼县| 芒康县| 南岸区| 固始县| 扬州市| 望都县| 革吉县| 辉县市| 和平区| 富平县| 三原县| 莆田市| 惠水县| 明光市| 塔城市| 米林县| 吕梁市| 阳山县| 彝良县| 汽车| 武清区| 霍邱县| 桑植县| 永寿县|