急求!!!!在线等JAVA编程 10
要求:在Person类覆盖(重写)toString方法,实现人的打印输出,输出格式是:[id]-姓名-年龄,如:[3]-张三-33。
Person.java
public class Person {
private int id;
private String name;
private int age;
@Override
public String toString(){ 展开
Person.java:
public class Person {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "[" + this.id + "]-" + this.name + "-" + this.age;
}
}
测试类:
public class T {
public static void main(String[] args) {
Person person = new Person();
person.setId(3);
person.setName("张三");
person.setAge(33);
System.out.println(person.toString());
}
}
运行结果:
2024-05-14 广告