superclass(base class)和subclassjava
擴展一個類的語法程序員
public class subClassName extends superClassName
子類中經過super關鍵字來調用基類的構造器和方法c#
重寫方法:在子類中定義一個簽名徹底同樣的方法,即實現了重寫(與c#不一樣的是,c#必須顯式的使用override修飾符或者使用new修飾符)數組
私有方法和靜態方法不能被重寫。若是在子類中定義了一個簽名徹底同樣的靜態方法,那麼父類中的靜態方法被隱藏,能夠經過SuperClassName.StaticMethoName來調用父類的方法ide
重載的意思是定義多個名稱相同但簽名不一樣的方法,重寫意思是在子類中對一個方法進行從新實現spa
爲了不出錯,能夠在重寫的方法前加上@Override註釋,代表這個方法要重寫基類中的方法,若是類型中不存在這個方法,編譯器會給出錯誤提示,防止拼寫錯誤,以下code
public class Circle extends GeometricObject { //....... @Override public String toString() { //........ } }
多態意味着子類型的變量適用於基類型對象
當調用一個對象的方法時,若是它的多個基類中都有這個方法,Java會沿着繼承鏈從高級往低級找,一旦找到,就不會繼續日後找,執行第一個找到的方法,即具體調用哪一個類的方法,是在運行時動態決定的,因此稱爲動態綁定blog
判斷對象是否某一類型的實例,可經過以下語法排序
if (myObject instanceof MyClass) { //................ }
相似c#中的以下寫法
if (myObject is MyClass) { //................ }
Object.toString()返回 類名@內存地址
ArrayList<E>用於存儲可變長度的數組,聲明ArrayList<E>的語法以下
ArrayList<java.util.Date> dataList = new ArrayList<java.uti.Date>(); //或者 ArrayList<java.util.Date> dataList = new ArrayList<>();//JDK 7及之後版本的簡寫方法
java.util.Arrays.sort(array);//數據排序 java.util.Collections.sort(arrayList);//ArrayList排序
ArrayList的泛型類型不能是原始類型,必須是對象類型
ArrayList<int> intList = new ArrayList<int>();//錯誤 ArrayList<Integer> intList = new ArrayList<Integer>();//正確
//Array轉ArrayList String[] array = {"red", "yellow", "blue"}; ArrayList<String> list = new ArrayList<>(Arrays.asList(array)); //ArrayList轉Array String[] array1 = new String[list.size()]; list.toArray(array1); //排序 Collections.sort(list); //最大值 Collections.max(list); //最小值 Collections.min(list); //混排 Collections.shuffle(list);
聲明異常:聲明異常更多的做用是告訴調用者,這個方法有可能拋出哪些異常,好讓調用者有所準備,進行異常處理(c#中沒有這個功能)
public void MayThrowException() throws Exception...... { }
RuntimeException和Error屬於非檢測異常(unchecked exception),其餘的異常屬於檢測異常(checked exception),編譯器會強制程序員在要麼在try...catch..中處理這些異常,要麼在方法頭聲明這些異常
一般當方法內部有throw....語句的時候,須要在方法頭上加上throws, 若是方法內部用try....cactch..處理了異常,就不須要加throws