類與對(duì)象

1.什么是類

  • 類是具有相同的狀態(tài)和相同的行為的一組對(duì)象的集合。例如:書(shū)、

電腦、人、老師,像這樣特指一類的名稱就是類。

1.1. 類和對(duì)象是什么關(guān)系

類和對(duì)象的關(guān)系就如同模具和用這個(gè)模具制作出來(lái)的物品之間的關(guān)

系。一個(gè)類給出它的全部對(duì)象的一個(gè)統(tǒng)一的定義,而它的每個(gè)對(duì)象

則是符合這種定義的一個(gè)實(shí)體(也稱作實(shí)例),因此類和對(duì)象的關(guān)

系就是抽象和具體的關(guān)系。

2 .什么是對(duì)象

  • 萬(wàn)事萬(wàn)物皆對(duì)象

  • 對(duì)象強(qiáng)調(diào)的是一個(gè)具體的個(gè)體

例如:楊樹(shù)就不是對(duì)象,我家門(mén)前第一顆楊樹(shù)就是對(duì)象。

2.1.對(duì)象由什么構(gòu)成

  • 對(duì)象由 狀態(tài) 和 行為 構(gòu)成

  • 對(duì)象的狀態(tài)是指對(duì)象的數(shù)據(jù),對(duì)象的狀態(tài)由變量表示,也叫對(duì)象的屬性。

  • 對(duì)象的行為是指對(duì)象的功能,對(duì)象的行為由方法表示。

2.2.對(duì)象是怎么創(chuàng)建的

-對(duì)象是由類實(shí)例化時(shí)創(chuàng)建的,因此創(chuàng)建對(duì)象必須先定義類。

  • 類是對(duì)象的模板,類中定義了什么屬性和方法,由這個(gè)類實(shí)例化的對(duì)象就有什么屬性和方法。

  • 類是一次性定義,對(duì)象可以多次創(chuàng)建。

3 代碼定義類

3.1.類的構(gòu)成

  • 類是有屬性與方法構(gòu)成的

示例: 定義學(xué)生類,屬性包括名字與成績(jī),方法有自我簡(jiǎn)紹

public class Student1 {
    String name; //類的屬性
    int  score;

    //類的構(gòu)造方法
    public Student1(String name,int  score){
        this.name = name;
        this.score = score;
    }
  //類的自我簡(jiǎn)紹方法
    public void  speak(){
        System.out.println("我的名字是:" + this.name +",這次考試成績(jī)是:"+ this.score);
    }
}

4.實(shí)例化對(duì)象

  • 通過(guò)類創(chuàng)建對(duì)象的過(guò)程稱為類的實(shí)例化。實(shí)例化的結(jié)果是產(chǎn)生了一個(gè)實(shí)例,實(shí)例也叫對(duì)象。
  • 通過(guò) new的方法創(chuàng)建實(shí)例化對(duì)象
Student s1 =new Student();
Student s2 =new Student();

5.對(duì)象的調(diào)用屬性

  • 對(duì)象調(diào)用屬性和方法是使用成員運(yùn)算符 . 來(lái)完成的
        Student s1 =new Student();
        s1.name="haha";
        s1.score=78;

        Student s2 =new Student();
        s2.name="yaya";
        s2.score=98;

6.類的構(gòu)造方法

  • 構(gòu)造方法也叫構(gòu)造函數(shù),或者叫構(gòu)造器
    1. 構(gòu)造方法的方法名必須與類名相同
    2. 構(gòu)造方法沒(méi)有返回值,也不寫(xiě)void
      例如:
class Student {
    //構(gòu)造方法
   public  Student(String name,int score){
        this.name = name;
        this.score = score;
    }
}

6.1構(gòu)造方法的調(diào)用

class Student {
    String name;
    int score;
    String no;

    /**
     * 有兩個(gè)參數(shù)的構(gòu)造方法
     * @param name1
     * @param score1
     */
    Student(String name1,int score1){
        name= name1;
        score= score1;
    }
}

6.2.構(gòu)造方法的方法重載

  • 方法名相同 參數(shù)不同 與返回值無(wú)關(guān)
class Student {
    String name;
    int score;
    String no;

    //第1種構(gòu)造方法的重載
    Student(String name1,int score1){
        name= name1;
        score= score1;
    }
    //第2種構(gòu)造方法的重載
    Student(String name1, int score1, String no1) {
        name = name1;
        score = score1;
        no = no1;
    }
}
public class StudentTest {
    public static void main(String[] args) {
        Student s1 =new Student("haha",76);//調(diào)用第1種構(gòu)造方法的重載
        Student s2 =new Student("yaya",67,"111");//調(diào)用第2種構(gòu)造方法的重載
    }
}

6.3.構(gòu)造方法的調(diào)用

  • 構(gòu)造方法是在實(shí)例化對(duì)象時(shí)調(diào)用的。并且實(shí)例化時(shí)傳遞的參數(shù)必須有構(gòu)造方法的參數(shù)一致。
 public static void main(String[] args) {
    Student s1 =new Student("haha",76);
    s1.Student("haha",88);//錯(cuò)誤的,對(duì)象名不允許調(diào)用構(gòu)造函數(shù)  
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容