JAVA複習筆記02

1六、interface中的成員變量默認爲public static final類型,方法只能是public(默認爲public)html

 

1七、內部類訪問外部類成員:java

Outer.this.num;

 

1八、一道有趣的題目函數

  使用內部類實現該程序。(答案附在本文末尾)this

interface Inter {
    void show();
}

class Outer {
    /*
    */
}

public class Test {
    public static void main(String[] args) {
          Outer.method().show();
      }
}    

 

1九、實例化內部類的方式spa

Outer.Inner inner=new Outer().new Inner();

  靜態內部類code

Outer.Inner inner=new Outer.Inner();

 

20、泛型的使用htm

  類中使用blog

class Test<T>{}

  方法中使用get

public <T,S extends T> void testDemo(T t,S s){}

  這樣定義時,要求傳入的S必需要是T的子類源碼

 

  這樣寫也無妨:

public <T,S extends T> void testDemo(){}

  只是沒有卵用,由於在裏面定義的T的變量不能初始化。

 

2一、通配符

public void testDemo(List<?> s)

  它等價於

public <T> void testDemo(List<T> s)

  通配符只在修飾變量中用到。

 

2二、異常處理

  throw扔出異常,若是本方法中catch了,就處理,沒有就扔到上級方法。

  finally會在返回前,把返回值壓棧而後執行,執行完後彈出返回。(延伸連接:http://www.javashuo.com/article/p-yamdkqhb-cx.html

 

2三、float z=12.14f; //沒有f會被看作是double而報錯

 

2四、System.out 是一個PrintStream(你能夠經過查看源碼來了解它是如何運行的)

 

2五、java標識符:52個字母,數字,下劃線,美圓符$;不能數字開頭,不能是關鍵字,不能有空格

 

2六、邏輯表達式的返回值

false:1:'a'

  若是兩邊同類型,則返回該類型。

  若是一邊是byte,short,char,另外一邊是int,且不溢出,則結果爲byte,short,char類型。

  不符合上述條件,隱式轉換爲高精度類型。

 

2七、switch支持的六種類型

byte short char int enum String(java7後)

 

2八、函數調用參數爲null時,會調用參數最子類的方法,若是最底層有2個或以上的兄弟類,則會報錯。

  參考連接:http://www.javashuo.com/article/p-bryeboqf-ds.html

 

2九、協變

  父類的一個方法返回另外一個類的父類;

  子類的這個方法返回另外一個類的子類。

  例:

  

class Flower {
    Plant kind() {
        return new Plant();
    }
}


class Luoyangred extends Flower {
    Peony kind() {
        return new Peony();
    }
}

 

30、interface中可定義內部類,默認爲public static

 

附:18的答案

  

interface Inter {
    void show();
}

class Outer {
    public static Inter method(){
        return new Inter(){
            public void show(){
                System.out.println("oo");
            }
        };
    }
}

public class Test {
    public static void main(String[] args) {
          Outer.method().show();
      }
}
相關文章
相關標籤/搜索