2.10 小結練習(多線程之前)

1.多態,向上轉型,方法,編譯看左,運行看右(動態綁定)

1

2.多態,向上轉型,方法,編譯看左,運行看右(動態綁定)

2

3.多態,向上轉型,方法,編譯看左,運行看右(動態綁定)

3

4.多態,向上轉型,變量,都看左

4

5.匿名內部類

5

6.非靜態的內部類中不能定義靜態成員

6

7.覆蓋,覆蓋方法權限要大於被覆蓋的,參數列表和返回值類型都要相同,參數列表不同就是子類特有方法。返回值類型不同,就會出錯,因爲調用不確定性。

7

8.

8

this:
代表當前對象,就是所在函數所屬對象的引用。

final:
1.final可以修飾類,方法,變量
2.final修飾的類不可以繼承
3.final修飾的方法不可以被覆蓋
4.final修飾的變量是一個常量,只能賦值一次。
  ①使用final和直接使用9區別在於,可以取常量名,加強閱讀性。
  ②final只固定變量的顯示初始化值,所以必須顯示賦值。
  ③一般都會加靜態static。如果再加publicpublic static final int x = 4;   這稱爲全局常量。

9.多態

9

10.接口的實現

10

11.異常:捕捉

11

12.對象的初始化過程

12

13.覆蓋

13

14.拋出異常語句下不能有任何語句存在。

14

15.非靜態的內部類在主函數中無法創建對象,聲明成靜態後,可以通過外部類調用new Demo.Inner();
或者new Demo().new Inner();

15

16.異常捕捉多catch問題

16

17.匿名內部類

17

18.異常:捕捉

18

19.單或和雙或的區別:

19

20.接口:定義額外的功能

/*
練習一:

                建立一個圖形接口,聲明一個面積函數,圖形和矩形都實現這個接口,並得出兩個圖形的面積。
                注:體現面向對象的特徵,對數值進行判斷。用異常處理。不合法的數值要出現 “ 這個數值是非法的 ”提示,不再進行運算。

*/


interface Areable      //圖形面積計算接口
{
    public static final double PI = 3.14; 
    public abstract double getArea();       //面積計算函數
}




class outOfAreaException extends RuntimeException   //數值非法異常類
{
        outOfAreaException()
        {

        }
        outOfAreaException(String msg)
        {
            super(msg);
        }
}




class Circular implements Graph        //圓形類
{
    private double r;         //圓半徑

    Circular(double r)
    {
        if(r<0)
            throw new outOfAreaException("這個數值是非法的");  //當半徑小於0,拋出數值非法異常
        this.r = r;
    }

    public double getArea()          //實現接口的抽象方法
    {
            return PI*Math.pow(r,2);
    }
}




class Rec implements Graph       //矩形類
{
    private double length,width;               //定義長寬,順序無所謂

    Rec(double length, double width)
    {
        if( length<0 ||  width<0)
                throw new outOfAreaException("這個數值是非法的");  //當半徑小於0,拋出數值非法異常
            this.length =length;
            this.width =width;
    }

    public double getArea()       //實現接口的抽象方法
    {           
                return length*width;
    }
}




class Practice1
{
    public static void main(String[] args)
    {

         Graph g =  new Circular(-5.6);   //初始化時如果數據存在負值,會拋出數值非法異常

        double area = g.getArea();  
        System.out.println(g.getClass().getName() + "'s area is " +area);   

    }

}
  1. 繼承
/*
        練習二:
                        假如我們在開發一個系統時需要對員工進行建模,員工包含3個屬性:姓名,工號,工資。
                        經理也是員工,除了含有員工的屬性外,還有獎金屬性。
                        請使用繼承的思想設計出員工類和經理類。要求類中提供必要的方法進行屬性訪問。

*/

class Employee    //員工類
{
    private String name,eNum;
    private int wages;
    Employee(String name,String eNum, int wages)
    {
        this.name = name;
        this.eNum = eNum;
        this.wages = wages;
    }

    void print()
    {
        System.out.print(name + "的工號是:"  + eNum + ", 每月工資:" + wages + "元。");
    }

}

class Manager extends Employee  //經理類 
{
         private int bonus;
        Manager(String name,String eNum, int wages, int bonus)
        {
            super(name,eNum,wages);
            this.bonus = bonus;
        }
        void print()
    {
            System.out.println();
            super.print();          
            System.out.print("獎金:" + bonus + "元。");

    }
}



class Practice2 
{
    public static void main(String[] args) 
    {
        Employee e = new Employee("張三","20143921005",8000);
        e.print();
        Manager m = new Manager("劉勇","20092013004",12000,6000);
        m.print();
    }
}
  1. 異常拋出:運行時異常 不需要聲明
/*
        在一個類中編寫一個方法,這個方法搜索一個字符數組中是否存在某個字符,如果存在,則返回這個字符在數組中第一次出現的
        位置(序號從0開始計算),否者,返回-1。
        要搜索的字符數組和字符都以參數形式傳遞給該方法,如果傳入的數組是null,應拋出IllegalArgumentException不合法參數異常。
        在類的main方法中以各種可能出現的情況測試驗證該方法編寫的是否正確。例如:
        字符不存在,字符存在,傳入的數組爲null。
*/

class ArrLookup
{
     int getIndex(char[] arr, char key)
    {
         if(arr == null)
                throw new IllegalArgumentException("不合法參數異常");
        for (int i = 0;i < arr.length;i++ )
        {
                if(arr[i] == key)
                    return i;
        }
        return -1;
     }
}


class Practice3 
{
    public static void main(String[] args) 
    {
        char[] arr = {'c','h','e','n','z','h','o','n','g','q','i','a','n'};
        ArrLookup a = new ArrLookup();

        System.out.println(a.getIndex(null,'a'));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章