1. 程式人生 > 實用技巧 >2020/08/19

2020/08/19

----類和物件的建立---

1.關於 “類”

類由2部分組成,即①屬性②方法(函式)

寫一個類(類是抽象的,相當於 人類,物件是類的具體化,比如說 蘇解)

public class Student{
    //屬性
    String name;
    int age;
    //方法
    public void study(){
        xxx...;
    }
}


2.關於”物件“

寫一個物件(主函式在這裡面)

public class Application{
    public static void main(String[] args) {
        Student sujie=new Student();//在Student類裡面,new出來了一個物件叫 sujie
        Student wuyuqing=new Student;//在Student類裡面,new出來了一個物件叫 wuyuqing
        sujie.name="蘇解";
        sujie.age=23;
        System.out.println(sujie.name);
        System.out.println(sujie.age);
    }
}

3.總結

  1. 類:抽象的 ; 物件:例項化的。
  2. 類例項化後會返回一個自己的物件(new Student輸完後按alt+回車)。
  3. sujie這個物件就是Student這個類的具體例項。