友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

Java编程思想第4版[中文版](PDF格式)-第151章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!




排列方式造成的。若将一个Circle (圆)造型到一个Shape (几何形状),就叫做上溯造型,因为圆只是几 

何形状的一个子集。反之,若将Shape 造型至 Circle,就叫做下溯造型。然而,尽管我们明确知道Circle 

也是一个Shape,所以编译器能够自动上溯造型,但却不能保证一个Shape 肯定是一个 Circle。因此,编译 

器不允许自动下溯造型,除非明确指定一次这样的造型。  

RTTI 在Java 中存在三种形式。关键字 instanceof告诉我们对象是不是一个特定类型的实例(Instance 即 

 “实例”)。它会返回一个布尔值,以便以问题的形式使用,就象下面这样:  

if(x instanceof Dog)  

((Dog)x)。bark();  

将x 造型至一个 Dog 前,上面的 if语句会检查对象x 是否从属于 Dog 类。进行造型前,如果没有其他信息可 

以告诉自己对象的类型,那么instanceof 的使用是非常重要的——否则会得到一个ClassCastException 违 

例。  

我们最一般的做法是查找一种类型(比如要变成紫色的三角形),但下面这个程序却演示了如何用 

instanceof标记出所有对象。  

  

//: PetCount。java  

// Using instanceof  

package c11。petcount;  

import java。util。*;  

  

class Pet {}  

class Dog extends Pet {}  

class Pug extends Dog {}  

class Cat extends Pet {}  

class Rodent extends Pet {}  

class Gerbil extends Rodent {}  

class Hamster extends Rodent {}  

  

class Counter { int i; }  

  

public class PetCount {  

  static String'' typenames = {  

    〃Pet〃; 〃Dog〃; 〃Pug〃; 〃Cat〃;  

    〃Rodent〃; 〃Gerbil〃; 〃Hamster〃;  

  };  

  public static void main(String'' args) {  

    Vector pets = new Vector();  

    try {  

      Class'' petTypes = {  

        Class。forName(〃c11。petcount。Dog〃);  

        Class。forName(〃c11。petcount。Pug〃);  

        Class。forName(〃c11。petcount。Cat〃);  

        Class。forName(〃c11。petcount。Rodent〃);  

        Class。forName(〃c11。petcount。Gerbil〃);  

        Class。forName(〃c11。petcount。Hamster〃);  



                                                                                          337 


…………………………………………………………Page 339……………………………………………………………

      };  

      for(int i = 0; i 《 15; i++)  

        pets。addElement(  

          petTypes'  

            (int)(Math。random()*petTypes。length)'  

            。newInstance());  

    } catch(InstantiationException e) {}  

      catch(IllegalAccessException e) {}  

      catch(ClassNotFoundException e) {}  

    Hashtable h = new Hashtable();  

    for(int i = 0; i 《 typenames。length; i++)  

      h。put(typenames'i'; new Counter());  

    for(int i = 0; i 《 pets。size(); i++) {  

      Object o = pets。elementAt(i);  

      if(o instanceof Pet)  

        ((Counter)h。get(〃Pet〃))。i++;  

      if(o instanceof Dog)  

        ((Counter)h。get(〃Dog〃))。i++;  

      if(o instanceof Pug)  

        ((Counter)h。get(〃Pug〃))。i++;  

      if(o instanceof Cat)  

        ((Counter)h。get(〃Cat〃))。i++;  

      if(o instanceof Rodent)  

        ((Counter)h。get(〃Rodent〃))。i++;  

      if(o instanceof Gerbil)  

        ((Counter)h。get(〃Gerbil〃))。i++;  

      if(o instanceof Hamster)  

        ((Counter)h。get(〃Hamster〃))。i++;  

    }  

    for(int i = 0; i 《 pets。size(); i++)  

      System。out。println(  

        pets。elementAt(i)。getClass()。toString());  

    for(int i = 0; i 《 typenames。length; i++)  

      System。out。println(  

        typenames'i' + 〃 quantity: 〃 +  

        ((Counter)h。get(typenames'i'))。i);  

  }  

} ///:~  

  

在Java 1。0 中,对 instanceof 有一个比较小的限制:只可将其与一个已命名的类型比较,不能同Class 对 

象作对比。在上述例子中,大家可能觉得将所有那些instanceof 表达式写出来是件很麻烦的事情。实际情况 

正是这样。但在Java 1。0 中,没有办法让这一工作自动进行——不能创建Class 的一个Vector,再将其与 

之比较。大家最终会意识到,如编写了数量众多的 instanceof表达式,整个设计都可能出现问题。  

当然,这个例子只是一个构想——最好在每个类型里添加一个static数据成员,然后在构建器中令其增值, 

以便跟踪计数。编写程序时,大家可能想象自己拥有类的源码控制权,能够自由改动它。但由于实际情况并 

非总是这样,所以 RTTI 显得特别方便。  

  

1。 使用类标记  

PetCount。java 示例可用Java 1。1 的类标记重写一遍。得到的结果显得更加明确易懂:  

  

//: PetCount2。java  

// Using Java 1。1 class literals  



                                                                                          338 


…………………………………………………………Page 340……………………………………………………………

package c11。petcount2;  

import java。util。*;  

  

class Pet {}  

class Dog extends Pet {}  

class Pug extends Dog {}  

class Cat extends Pet {}  

class Rodent extends Pet {}  

class Gerbil extends Rodent {}  

class Hamster extends Rodent {}  

  

class Counter { int i; }  

  

public class PetCount2 {  

  public static void main(String'' args) {  

    Vector pets = new Vector();  

    Class'' petTypes = {  

      // Class literals work in Java 1。1+ only:  

      Pet。class;  

      Dog。class;  

      Pug。class;  

      Cat。class;  

      Rodent。class;  

      Gerbil。class;  

      Hamster。class;  

    };  

    try {  

      for(int i = 0; i 《 15; i++) {  

        // Offset by one to eliminate Pet。class:  

        int rnd = 1 + (int)(  

          Math。random() * (petTypes。length 1));  

        pets。addElement(  

          petTypes'rnd'。newInstance());  

      }  

    } catch(InstantiationException e) {}  

      catch(IllegalAccessException e) {}  

    Hashtable h = new Hashtable();  

    for(int i = 0; i 《 petTypes。length; i++)  

      h。put(petTypes'i'。toString();  

        new Counter());  

    for(int i = 0; i 《 pets。size(); i++) {  

      Object o = pets。elementAt(i);  

      if(o instanceof Pet)  

        ((Counter)h。get(  

          〃class c11。petcount2。Pet〃))。i++;  

      if(o instanceof Dog)  

        ((Counter)h。get(  

          〃class c11。petcount2。Dog〃))。i++;  

      if(o instanceof Pug)  

        ((Counter)h。get(  

          〃class c11。petcount2。Pug〃))。i++;  

      if(o instanceof Cat)  



                                                                                          339 


…………………………………………………………Page 
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!