5.9java-new

232 阅读1分钟

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='开朗'}