Java學習筆記--繼承和多態(下),java學習筆記

來源:互聯網
上載者:User

Java學習筆記--繼承和多態(下),java學習筆記

1.通過繼承來開發超類(superclass)

2.使用super 關鍵詞喚起超類的構造方法

3.在超類中覆蓋方法

4.區分override和overload

5.在Object類中探索toString()類

6.發現多態性和動態綁定

7.描述解釋為什麼向下轉型是必須的

8.在Object類中探索equals 方法

9.儲存,取回,實現ArrayList的對象

10.使用ArrayList類實現Stack

11.超類中使用資料和方法,protected

12.使用final模組來禁止類和方法的覆蓋

 

9.儲存,取回,實現ArrayList的對象

ArrayList類,這個類對於儲存物件來說很有用,你可以建立數組來儲存物件,但是一旦數組建立完畢,它的大小已經固定了。Java提供了ArrayList類用來儲存不限量的對象。

ArrayList 方法區

ArrayList() 建立一個空的類               
add(o:Object): void 在列表的末尾加入一個元素
add(index: int, o:Object): void 在列表的某個特殊位置加入對象
clear():void 將列表清空
contains(o: Object):boolean 是否包含某個對象
get(index: int):Object 返回某個位置的元素
indexOf(o: Object ) : int  返回某個元素的位置
isEmpty():boolean 判斷是否為空白的列表
lastIndexOf(o: Object): int 最後一個對象的位置
remove(o : Object): boolean 匹配元素並從表中移除
size(): int 表的大小
set(index: int, o:Object):Object 設定某個位置上的元素

TestArrayList.java

public class TestArrayList {    public static void main(String[] args){        java.util.ArrayList cityList = new java.util.ArrayList();                cityList.add("Beijing");        cityList.add("Shanghai");        cityList.add("Tianjin");        cityList.add("Xiamen");        cityList.add("Fuzhou");        System.out.println("List Size?"+cityList.size());        System.out.println("Is Guangzhou in the list ? The answer is "+cityList.contains("Guangzhou"));        System.out.println("The location of Shanghai in the list? The answer is "+cityList.indexOf("Shanghai"));        System.out.println("Is the list is empty? The answer is "+ cityList.isEmpty());                cityList.add(2, "Hangzhou");        cityList.remove("Tianjin");                cityList.remove(1);        System.out.println(cityList.toString());                for(int i = cityList.size()-1 ;i>=0;i--)            System.out.println(cityList.get(i)+" ");    }}

顯示結果如下

這個對象當成字串數組來使用,如果說儲存物件了?

添加以下代碼

list.add(new Circle(1, 2.5));list.add(new Circle(2, 15.5)); for (int i = 0 ; i<2;i++)   System.out.println("The area of the circle"+((Circle) list.get(i)).number() + " is + "+ (Circle)list.get(i).GetArea() );

在原本沒進行類型轉換的時候會發現編譯器報錯了

關於類型轉換可以參考以前的筆記。

10.使用ArrayList類實現Stack

關於棧的使用,Stack棧也是一種線性表,只不過這種線性表比較特殊,只能在表尾進行插入和彈出,對上面的例子變更,push和pop,只要在push方法中調用對應的方法。

資料區域

存對象

size

方法區

push: 找到隊尾進行插插入 

pop:找到隊尾,進行get(index: int):Object並刪除隊尾 +remove(index: int): boolean

GetTop: 按照ArrayList類的方法修改即可

11.超類中使用資料和方法,protected

如果想要子類獲得父類的資料和方法,非子類不能訪問這些資料和方法。

將前面所學 public private protected

public 地區塊塊可以修改,pirvate 的地區塊 子類不能夠直接存取, protected的地區塊可見但是不可改。

資料和方法的可見度

  其他類 相同包 子類   不同包 
public true true true true
protected true true true --
defaul true true -- --
private true -- -- --

 

 

 

 

12.使用final模組來禁止類和方法的覆蓋

    final 關鍵詞

  

public fina class c{.....}
public class Test{  public final  void main(){          .......    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.