(1)编写Student类,包含学号ID,姓名NAME属性,并重写Object类的toString()方法,用于返回学号,姓名信息 (2)编写测试类,创建Student类对象,调用toString()方法输出信息。
时间: 2024-03-01 08:54:46 浏览: 59
好的,以下是代码示例:
```java
// Student类
public class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
// 测试类
public class TestStudent {
public static void main(String[] args) {
Student student = new Student("1001", "张三");
System.out.println(student.toString());
}
}
```
输出结果为:
```
Student{id='1001', name='张三'}
```
阅读全文