在說到物件導向程式之前,我們要先認識物件和類別
類別是定義物件的一種型態,沒有實體,裡面有定義好的屬性和方法,大略分為方法、類別方法和建構子
物件就是某一類別的實體
class Student{
    public String name;
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
}
public class Main{
    public static void main(String[] agrs){
        Student Peter = new Student();  // 物件
        Peter.setName("Peter");
        System.out.println(Peter.getName());
    }
}
然而我們所謂的物件導向程式設計就是去提高我們軟體的重用性、靈活性和擴充性,還有讓程式間可以互相存取相關的資料,其實就和前一天說到的方法一樣,有些功能可以重複利用,所以我們就去建立可以重複利用到的零件,等我們要用到的時候就可以省很多力
同時還有低耦合性,物件本身可以自由修改與變化,而且不影響其他物件本身的功能和運作
class Student{
    public Student(){
    }
    public Student(String name){
    }
}
this()表示呼叫物件的建構子
class Student{
    public String name;
    public setName(String name){
        this.name = "Max";   // this的name是指上面的name設定為setName方法參數的name
    }
}
class Student{
    public String name;
    static {
        System.out.println("Run static block")  // 只會列印出一次
    }
}
public class Main{
    public static void main(String[] agrs){
        Student Peter = new Student();  // 物件
        Student Max = new Student();  // 物件
        Student Steven = new Student();  // 物件
    }
}