【正文】
e)不一定是圓 (Circle)。 盡管子類的對象也“是”父類的對象,子類類型和父類類型卻是不相同的。子類對象可以被視為父類的對象。這是合理的,因為子類含有和每個超類成員相對應的成員。但也應認識到,子類通常擁有比父類更多的成員。但 Java 不允許逆向賦值,因為如果把超類對象賦給子類的引用,就會使子類中額外的子類成員沒有定義。 子類對象的引用可以被隱式地轉換為超類對象的引用,因為根據(jù)繼承性,子類對象“是”超類對象。 有四種可能的方法來使父類的對象和子類的對象與父類的引用和子類的引用相匹配: 父類對象與可以直接用父類的引用。 子類對象與可以直接用子類的引用。 子類的對象用父類的引用是安全的,因為子類對象同樣也是其父類的對象。但這樣的代碼只能引用父類的成員。如果該代碼通過父類的引用來訪問子類所特有的成員,編譯器就會報告一個語法錯誤。 如果用子類來引用父類的對象,將報告一個語法錯誤。 盡管將子類的對象看 成是父類的對象會帶來很大的便利,而且可以通過這些父類的引用操縱這些對象來實現(xiàn),但這會引發(fā)一個問題。在一個工資管理系統(tǒng),例如,我們希望能夠通過一組員工數(shù)組來計算每個人的周薪。但是直覺告訴人們,使用父類的引用將使程序只調用父類的工資計算程序(如果父類中確實有這樣一個程序的話)。我們需要一種方式僅通過使用父類的引用來正確的調用對每個對象(無論是父類對象或子類對象)相對應的工資計算程序。其實,這正是當我們考慮了多態(tài)性和動態(tài)鏈接后 Java 所呈現(xiàn)的并將在此章節(jié)中討論的內容。 我們可以使用繼承來定制現(xiàn)有的軟件。當我們使 用繼承來從現(xiàn)有類創(chuàng)建一個新類時,新類將繼承的原有類的屬性和行為,此時我們可以添加屬性和行為或重載父類的行為來定制能滿足我們需要的類。 很難讓學生意識到設計師和工業(yè)的大型軟件項目的實施者所面臨的問題。做過這類項目的人都會說提高軟件開發(fā)效率的最好方法是鼓勵對軟件的再利用。一般來說,面向對象的編程,像 Java,就可以實現(xiàn)。 實質和有用的類庫使得通過及繼承而實現(xiàn)的軟件重用實現(xiàn)利益最大化。人們對Java 類庫的興趣隨著 Java 的發(fā)展而增加。正如獨立軟件廠商生產的壓縮套裝的軟件伴隨著個人電腦的問世而出現(xiàn)爆炸性的增長 一樣, Java 類庫的制造和銷售也將呈現(xiàn)這樣一個趨勢。應用程序設計人員將使用這些類庫來構建他們的應用程序,而類庫的設計者會因為將其開發(fā)的類庫與應用程序打包在一起而得到收益。在不久的將來,隨著在大量領域的應用, Java 的類庫將會起到至關重要的作用并得到極大的發(fā)展 。 附件 2:外文原文 (復印件) Java Objectoriented programming Objectoriented programming and its key ponent technologies as inheritance and polymorphism. Software reusability saves time in program development. It encourages reuse of proven and debugged highquality software, thus reducing problems after a system bees operational. These are exciting possibilities. Polymorphism e nables us to write programs in a general fashion to handle a wide variety of existing and yettobespecified related makes it easy to add new capabilities to a system. Inheritance and polymorphism are effective techniques for dealing with software plexity. When creating a new class, instead of writing pletely new instance variables and instance methods, the programmer can designate that the new class is to inherit the instance variables and instance methods of a previously defined superclass. The new class is referred to as a subclass. Each subclass itself bees a candidate to be a superclass for some future subclass. The direct superclass of a class is the superclass from which the class explicitly inherits (via the keyword extends). An indirect superclass is inherited from two or more levels up the class hierarchy. For example, class JApplet (package ) extends class Applet (package ). With single inheritance, a class is derived from one superclass. Java does not support multiple inheritance (as C++ does) but it does support the notion of interfaces. interfaces help Java achieve many of the advantages of multiple inheritance without the associated problems. We will discuss the details of interfaces in this chapter. We consider both general principles and a detailed specific example of creating and using interfaces. A subclass normally adds instance variables and instance methods of its own, so a subclass is generally larger than its superclass. A subclass is more specific than its superclass and represents a smaller, more specialized group of objects. With single inheritance, the subclass starts out essentially the same as the superclass. The real strength of inheritance es from the ability to define in the subclass additions to, or replacements for, the features inherited from the superclass. Every subclass object is also an obj