第十章多態性
1、後綁定
一、引用變量的類型和該引用變量指向的對象必須是兼容的,但沒必要徹底相同;
二、多態性引用可以隨時間變化指向不一樣類型的對象;
三、對於有多態性引用,後綁定要延遲到程序運行時才能執行;php
四、後綁定的效率低於編譯階段的綁定效率。html
2、由繼承實現多態性
一、一個引用變量能夠指向由繼承關係的任何類的任何對象;
二、實際將調用的方法版本取決於對象的類型而不是引用變量的類型;git
3、利用接口實現多態性
一、接口名能夠用於聲明對象引用變量;
二、一個接口引用變量能夠指向實現該接口的任何類的任何對象;程序員
4、排序
一、選擇排序法;
二、插入排序法;
三、比較:選擇排序法和插入法的效率相同,但選擇法所執行的交換操做的次數更少。web
5、搜索
一、線性搜索;
二、二分搜索;
三、比較:二分搜索比線性搜索的效率高,但二分搜索要求數據已經作過排序。數據結構
這裏講解了靜態方法在什麼地方適用,而且也爲我闡明瞭一個概念,由於靜態綁定發生在代碼執行以前,所以這種綁定就不會延遲程序的執行時間。ide
public class Father { protected String name = "父親屬性"; } public class Son extends Father { protected String name = "兒子屬性"; public static void main(String[] args) { Father sample = new Son(); System.out.println("調用的屬性:" + sample.name); } }
最後的結論爲調用的成員爲父親的屬性。學習
如今若是試圖調用子類的成員變量name,該怎麼作?this
public class Father { protected String name = "父親屬性"; public String getName() { return name; } } public class Son extends Father { protected String name = "兒子屬性"; public String getName() { return name; } public static void main(String[] args) { Father sample = new Son(); System.out.println("調用的屬性:" + sample.getName()); } }
就是將將該成員變量封裝成方法getter形式。spa
可是這個是理解繼承裏的多態性,可是從總體來說,仍是沒有特別明細的去講出爲何?
另一我的從多態的概念和做用之間來說:
書上是這樣說的:
當一個子類重載了其夫類的方法的定義時,實際上該方法的兩個版本都存在。若是用一個多態性引用調用該方法,那麼被調用方法的版本由執行方法調用的對象的類型確度,而不是由引用變量的類型肯定.
錯題1
Inheritance through an extended (derived) class supports which of the following concepts?
A. interfaces
B. modular
C. information hiding
D. code reuse
E. correctness
正確答案: D 個人答案: A
解析:經過擴展一個類並繼承它,新類沒必要從新實現任何這些繼承的方法或實例數據,從而節省了程序員的工做量。所以,代碼重用是爲了您的須要擴展它而重用其餘代碼的好處。
錯題2
Aside from permitting inheritance, the visibility modifier protected is also used to
A. permit access to the protected item by any class defined in the same package
B. permit access to the protected item by any static class
C. permit access to the protected item by any parent class
D. ensure that the class cannot throw a NullPointerException
E. define abstract elements of an interface
正確答案: A 個人答案: B
解析:受保護的可見性修飾符用於以受保護的方式控制對項目的訪問。保護是訪問限於當前類(如私人項目),同一包中的類或此類的擴展類。
錯題3
Which of the following is an example of multiple inheritance?
A. A computer can be a mainframe or a PC
B. A PC can be a desktop or a laptop
C. A laptop is both a PC and a portable device
D. A portable device is a lightweight device
E. Macintosh and IBM PC are both types of PCs
正確答案: C 個人答案: E
解析:多重繼承意味着一個給定的類繼承了多個父類。在上面列出的那些中,筆記本電腦繼承了來自PC和便攜式設備的特性。A,B和E中的答案都是單個繼承的例子,其中一個班級至少有兩個孩子(在A中,計算機有兒童大型機和PC,B,PC有兒童臺式機和筆記本電腦,E,PC有孩子Macintosh和IBM PC)。答案D表示一個類的屬性。
錯題4
Which of the following is true regarding Java classes?
A. All classes must have 1 parent but may have any number of children (derived or extended) classes
B. All classes must have 1 child (derived or extended) class but may have any number of parent classes
C. All classes must have 1 parent class and may have a single child (derived or extended) class
D. All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes
E. All classes can have either 0 or 1 parent class or any number of children (derived or extended) classes
正確答案: A 個人答案: E
解析:Java支持繼承,但不支持多重繼承,因此Java類能夠有任意數量的子元素,但只有一個父元素。此外,因爲全部的Java類都直接或間接地從Object類繼承,全部的Java類都只有一個父類。
錯題5
A variable declared to be of one class can later reference an extended class of that class. This variable is known as
A. protected
B. derivable
C. closeable
D. polymorphic
E. none of the above, a variable declared to be of one class can never reference any other type of class, even an extended class
正確答案: D 個人答案: A
解析:多態意味着變量能夠有多種形式。在普通狀況下,Java被強烈定義,即一旦聲明爲某個類型的變量永遠不會變爲不一樣的類型。這是一個例外,多態變量能夠是任何類型的派生類(儘管不是同時,變量能夠從一種類型變爲另外一種類型)。
錯題6
Using the reserved word, super, one can
A. access a parent class ‘constructor(s)
B. access a parent class ‘methods and instance data
C. access a child class ‘constructor(s)
D. access a child class ‘methods and instance data
E. none of the above
正確答案: E 個人答案: B
解析:super保留字提供了訪問父類的方法和實例數據(無論它們是否隱藏)的機制。另外,可使用super訪問父類的構造器。因此正確的答案是A和B的組合,這不是一個選項,因此正確答案是E.
錯題7
Why shouldn't an abstract method be declared final?
A. There's nothing wrong with doing so
B. Abstract methods cannot be overridden and they must be if a concrete class ever is to be instantiated
C. So long as the Abstract method never actually is used in by any other method, there's no problem with doing this
D. As long as the Abstract method is declared in a Class (not an Interface), there's nothing wrong with doing this
E. None of the above
正確答案: B 個人答案: E
解析:爲了使抽象方法變得具體,它必須被覆蓋。聲明一個方法是最終的,所以不可能覆蓋它。這是一個矛盾,是被禁止的。
錯題8
If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but cannot redefine x to be a different type.
A. true
B. false
正確答案: B 個人答案: A
解析:派生類能夠從新定義父類的任何實例數據或方法。父類的版本如今隱藏了,可是能夠經過使用super來訪問,就像在super.x中同樣。
錯題9
The reserved word, extends, is used to denote a has-a relationship.
A. true
B. false
正確答案: B 個人答案: A
解析:Extends用於繼承...繼承是一個is-a關係,而不是一個has-a關係。
錯題10
Although classes can be inherited from one-another, even abstract classes, interfaces cannot be inherited.
A. true
B. false
正確答案: B 個人答案: A
解析:接口具備普通類所具備的全部繼承屬性。所以,您能夠建立一個Interface繼承層次結構,就像您能夠建立一個Class繼承層次結構同樣。然而,你不能作的是實例化他們必須實現的接口。
代碼調試中的問題和解決過程, 一個問題加1分
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | |
---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 |
第一週 | 156/156 | 1/1 | 15/15 |
第二週 | 217/371 | 1/2 | 20/35 |
第三週 | 233/604 | 2/4 | 20/55 |
第四周 | 1382/1986 | 1/5 | 35/90 |
第五週 | 146/2196 | 1/6 | 25/115 |
第六週 | 462/2658 | 1/7 | 15/130 |
第七週 | 856/3514 | 1/8 | 20/150 |
第八週 | 1877/5391 | 3/11 | 20/170 |