20172329 2017-2018-2 《程序設計與數據結構》第八週學習總結

20172329 2017-2018-2 《程序設計與數據結構》第八週學習總結

教材學習內容總結

第十章多態性
1、後綁定
一、引用變量的類型和該引用變量指向的對象必須是兼容的,但沒必要徹底相同;
二、多態性引用可以隨時間變化指向不一樣類型的對象;
三、對於有多態性引用,後綁定要延遲到程序運行時才能執行;php

四、後綁定的效率低於編譯階段的綁定效率。html

2、由繼承實現多態性
一、一個引用變量能夠指向由繼承關係的任何類的任何對象;
二、實際將調用的方法版本取決於對象的類型而不是引用變量的類型;git

3、利用接口實現多態性
一、接口名能夠用於聲明對象引用變量;
二、一個接口引用變量能夠指向實現該接口的任何類的任何對象;程序員

4、排序
一、選擇排序法;
二、插入排序法;
三、比較:選擇排序法和插入法的效率相同,但選擇法所執行的交換操做的次數更少。web

5、搜索
一、線性搜索;
二、二分搜索;
三、比較:二分搜索比線性搜索的效率高,但二分搜索要求數據已經作過排序。數據結構

教材學習中的問題和解決過程

  • 問題1:在第一節所講到的綁定中,說:「編譯時綁定的效率比動態綁定的效率高」,爲何?
  • 問題1解決方案:
    首先,一開始個人想法就是說,由於編譯時的綁定叫作靜態綁定也叫作前期綁定,我就查找了相關的資料,其中,講到靜態綁定有這樣一個說法:

這裏講解了靜態方法在什麼地方適用,而且也爲我闡明瞭一個概念,由於靜態綁定發生在代碼執行以前,所以這種綁定就不會延遲程序的執行時間。ide

  • 問題2:動態綁定的範疇只有對象嗎,可不能夠是類的方法之類的?
  • 問題2解決方案:
    咱們利用繼承實現多態性作一個實驗來說:
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

  • 問題3:重寫和多態有什麼關係?
  • 問題3解決方案:由於在看到一個問題的討論上,看到了這樣的問題:爲何要說有方法重寫是多態的前提呢?其實一方面,看到這個問題的時候,本身意識到前提就是他們倆的一種關係,同時本身也想問這個問題,就繼續往下看了:
    在那篇討論裏,不少大佬用飛機舉例子,其中一我的這樣講:

可是這個是理解繼承裏的多態性,可是從總體來說,仍是沒有特別明細的去講出爲何?
另一我的從多態的概念和做用之間來說:

書上是這樣說的:

當一個子類重載了其夫類的方法的定義時,實際上該方法的兩個版本都存在。若是用一個多態性引用調用該方法,那麼被調用方法的版本由執行方法調用的對象的類型確度,而不是由引用變量的類型肯定.

代碼調試中的問題和解決過程

  • 問題1:在pp10.1的練習當中,起初不理解到底如何去編,我就先編了一個接口,以後,將Staff裏的void方法放到了接口裏,在main驅動裏學習書裏的例子,用接口名聲明一個對象,將其實例化以後,顯示有錯誤?


  • 問題1解決方案:我ALT加回車想着將Staff聲明爲接口類,試一試,就能夠了,可是好像不太符合題目要求,因此感受本身仍是錯的,可是的確能夠輸出。

代碼託管

上週考試錯題總結

錯題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繼承層次結構同樣。然而,你不能作的是實例化他們必須實現的接口。

結對及互評

  • 本週結對學習狀況
  • 博客中值得學習的或問題:
    • 內容詳略得當;
    • 代碼調試環節比較詳細;
  • 基於評分標準,我給本博客打分:4分。得分狀況以下:
  1. 正確使用Markdown語法(加1分):
  2. 模板中的要素齊全(加1分)
  3. 教材學習中的問題和解決過程, 一個問題加1分
  4. 代碼調試中的問題和解決過程, 一個問題加1分

  • 博客中值得學習的或問題:
    • 內容詳略得當;
    • 代碼調試環節比較詳細;
  • 基於評分標準,我給本博客打分:4分。得分狀況以下:
  1. 正確使用Markdown語法(加1分):
  2. 模板中的要素齊全(加1分)
  3. 教材學習中的問題和解決過程, 一個問題加1分
  4. 代碼調試中的問題和解決過程, 一個問題加1分

其餘

  • 這一週應該是快樂與痛苦相伴最契合的一週,在五一,老是在學習和休息之間劃分好時間,可是有時候太想休息老是投入不到學習中,使得本身變得懶散,最後讓本身玩也沒玩好,學也沒學好,因此我以爲合理支配時間會使咱們本身過的更加充分。
  • 時間過得很快,眼看大半學期就要過去了,Java課程也變得有點困難,終究是本身打的基礎不牢靠,老是學一點忘一點,再加上啦啦操訓練佔據了晚上大部分時間,和別人學習的時間差了不少,感受本身最近都不是由於玩手機熬夜了,天天晚上都是敲到電腦沒電,看着別人電腦還有電的無奈,只能看會兒書睡覺,如今班級裏的競爭愈來愈強,但願你們都加油,身體爲主,加油!

學習進度條

代碼行數(新增/累積) 博客量(新增/累積) 學習時間(新增/累積)
目標 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

參考資料

Java程序設計
藍墨雲
Java靜態綁定與動態綁定
方法重寫是多態的必須條件麼?

相關文章
相關標籤/搜索