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(){.......} }
?
轉載于:https://www.cnblogs.com/rynerlute/p/6396259.html
總結
以上是生活随笔為你收集整理的Java学习笔记--继承和多态(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP安全模式
- 下一篇: java美元兑换,(Java实现) 美元