8 類別

8.1 發現類別

例題: BangBuck.java (原始碼)

8.2 界定方法

    class Product {
        public Product() { ... }
        public void read() { ... }
        public boolean isBetterThan(Product b) { ... }
        public void print() { ... }
        implementation detail
    }
方法中有三類:
  1. 建構器(constructor): 設定新物件的初值, 如 Product()。
  2. mutator: 更改物件的量值, 如 read()。
  3. accessor: 讀取物件的量值,如 isBetterThan(...),print()。

例題: ProductTest1.java (原始碼)

8.3 個例變數

每一物件必須記得現況。 物件的現況儲存在一個或更多的個例變數(instance variable)中。 例如,類別 Product 的物件用三個個例變數描述:
    class Product{
       public Product();

       public void read();

       public boolean isBetterThan(Product b);
       public void print();

       private String name;
       private double price;
       private int score;
    }

8.4 方法的實現

類別中的每一方法都必須實現。

例題: ProductTest2.java (原始碼)

8.5 預設的建構器

建構器(constructor)用以設定一物件中的資料項初值。 建構器的名稱和類別相同。 建構器通常宣告為 public, 但是不規定回值型式。 假如一類別的宣告中,未含建構器,則 java 會替此類別加一預設的建構器,即沒有參數的建構器。 例如,
  public Product()
   {  name = "";
      price = 0;
      score = 0;
   }

8.6 有參數的建構器

一類別的建構器可以不只一個。 類別 Product 的建構器只有一個,但是類別 Employee 卻有兩個:
   public Employee() { name = ""; salary = 0; }
   public Employee(String n, double s) { name = n; salary = s; }
預設的建構器沒有參數,另一建構器有兩個參數。 凡是兩個函數有相同名稱,但不同型式的參數,稱為 overloaded。

假如一類別的資料項為一物件,如

class Employee {
   public Employee(String employeeName,
      int hireYear, int hireMonth, int hireDay) { ... }
   . . .
   private String name;
   private double salary;
   private Time hireDate;
}
hireDate 是屬於類別 Time 的物件。 在 Employee 的建構器中,設定 hireDate 的初值時,用 Time 的建構器, 如
   public Employee(String employeeName,
         int hireYear, int hireMonth, int hireDay) {
      name = employeeName;
      salary = 0;
      hireDate = new Time(hireYear, hireMonth, hireDay, 0, 0, 0);
   }

8.7 讀取物件的資料

一物件的私有資料,只有該物件中的方法可以讀取。 所有其他物件的方法,必須透過該物件中的公用方法。

例如, raiseSalary 不能直接讀寫 salary (類別 Employee 的私有資料):

    static void raiseSalary(Employee e, double percent) {
        e.salary = e.salary * (1 + percent/100);  // error
    }
而必須使用 getSalary 和 setSalary (均為類別 Employee 的公用方法):
    static void raiseSalary(Employee e, double percent) {
        double newSalary = e.getSalary() * (1 + percent/100);
        e.setSalary(newSalary);
    }

8.8 個例方法和類別方法

raiseSalary 是一類別方法(class method),
    static void raiseSalary(Employee e, double percent) {
        double newSalary = e.getSalary() * (1 + percent/100);
        e.setSalary(newSalary);
    }
假設 harry 是類別 Employee 的物件,調薪用
    raiseSalary(harry, 7);
改寫 raiseSalary 為個例方法:
    class Employee {
        public void raiseSalary(double percent) {
            salary = salary * (1 + percent/100);
        }
    }
調薪必須用
    harry.raiseSalary(7);

8.9 物件導向設計

  1. Finding classes
    Player
    Clock
    Time
    Level
    Game
    Round
  2. Finding interfaces
    Player : Player(name, initialLevel), incrementScore, getScore
    Clock : draw, setTime
    Time : Time(hours, minutes), getHours, getMinutes
    Game : play, readPlayerInformation, playRound
  3. Implementing interfaces
  4. Combining the classes into a program
    例題: ClockApplet.java (原始碼)
    測試 ClockApplet