...为Homework3的类中使用Person类带参数的构造方法
发布网友
发布时间:2024-05-12 18:35
我来回答
共1个回答
热心网友
时间:2024-06-16 20:45
参考代码:
class Person {
private String name;
private String sex;
private int age;
public Person() {
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "[name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class Homework3 {
public static void main(String[] args) {
Person p1 = new Person("小明", "男", 19);
Person p2 = new Person("小花", "女", 18);
Person p3 = new Person("小军", "男", 20);
System.out.println(p1 + "\n" + p2 + "\n" + p3);
}
}
执行结果: