2014年2月26日 星期三

Learning the Java Language (2) : Classes and Objects

Classes and Objects



Classes

class MyClass extends MySuperClass implements YourInterface {
    // fields
    public int cadence;
        
    // constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    // methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
}

//定義了MyClass類別,繼承了MySuperClass,並用了YourInterface的介面[?!]
//field為初始值設定,constrctor為類別主要的輸入及功能,method為類別提供的多種方法
 

1. 
public modifier—the field is accessible from all classes
private modifier—the field is accessible only within its own class
 
[variable names]
  • the first letter of a class name should be capitalized, and [類別名的首字母應該大寫]
  • the first (or only) word in a method name should be a verb. [method的首字母應該動詞]
2.
Java can distinguish between methods with different method signatures.
This means that methods within a class can have the same name
if they have different parameter lists
[只要輸入的參數不一樣,method名稱一樣沒關係(在同一class裡面),JAVA分得出來]
[Overloading Methods
 
3.
在寫constructor時,建議書寫一個完全給予引數的constructor
跟一個完全沒有input的constructor,確保輸出都有數值
 
不可以寫兩個一樣input的constructor,會編譯錯誤
 
 
 




沒有留言:

張貼留言