5.9java-new

啊剛發表於2018-05-09

java物件: 建立一個新的class寫基本要素:

public class Zyg { private String name;//名字 private int height;//身高 private int weight;//體重 private String Sex;//性別 private String hobby;//愛好 private String Features;//特徵 private String character;//性格

public Zyg(int height, int weight, String name, String sex, String hobby, String features, String character) {
    this.character = character;
    this.Features = features;
    this.height = height;
    this.hobby = hobby;
    this.name = name;
    this.Sex = sex;
    this.weight = weight;
}

public Zyg() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getHeight() {
    return height;
}

public void setHeight(int height) {
    this.height = height;
}

public int getWeight() {
    return weight;
}

public void setWeight(int weight) {
    this.weight = weight;
}

public String getSex() {
    return Sex;
}

public void setSex(String sex) {
    Sex = sex;
}

public String getHobby() {
    return hobby;
}

public void setHobby(String hobby) {
    this.hobby = hobby;
}

public String getFeatures() {
    return Features;
}

public void setFeatures(String features) {
    Features = features;
}

public String getCharacter() {
    return character;
}

public void setCharacter(String character) {
    this.character = character;
}


@Override
public String toString() {
    return "Zyg{" +
            "name='" + name + '\'' +
            ", height=" + height +
            ", weight=" + weight +
            ", Sex='" + Sex + '\'' +
            ", hobby='" + hobby + '\'' +
            ", Features='" + Features + '\'' +
            ", character='" + character + '\'' +
            '}';
}
複製程式碼

}

在函式中去呈現 應用new來引

public class main {

public static void main(String[] args) {

    Zyg zyg=new Zyg();
    zyg.setCharacter("開朗");//性格
    zyg.setFeatures("完美");//特徵
    zyg.setHeight(171);//身高
    zyg.setWeight(100);//體重
    zyg.setHobby("睡覺");//愛好
    zyg.setName("趙永剛");//名字
    zyg.setSex("男");//性別
    System.out.println(zyg.toString());

}
複製程式碼

}

列印結果為:

Zyg{name='趙永剛', height=171, weight=100, Sex='男', hobby='睡覺', Features='完美', character='開朗'}