最短代码实现请按照以下要求设计一个学生类Student,并进行测试。要求如下: (1)Student类中包含姓名(name)、成绩(score)两个私有属性;…
- User
最短代码实现请按照以下要求设计一个学生类Student,并进行测试。要求如下: (1)Student类中包含姓名(name)、成绩(score)两个私有属性; (2)分别给这两个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值; (3)Student类中定义一个无参的构造方法和一个接收两个参数的构造方法,两个参数分别为姓名和成绩属性赋值; (4)重写toString方法,输出Student对象信息,格式为:Student[name=jack, score=89]; (5)在测试类中创建两个Student对象,一个使用无参的构造方法,然后调用方法给姓名和成绩赋值为(tom,90),一个使用有参的构造方法,在构造方法中给姓名和成绩赋值为(jack,89)。
Assistant
class Student { private String name; private int score; // 无参构造方法 public Student() { } // 有参构造方法 public Student(String name, int score) { this.name = name; this.score = score; } // 设置姓名 public void setName(String name) { this.name = name; } // 获取姓名 public String getName() { return name; } // 设置成绩 public void setScore(int score) { this.score = score; } // 获取成绩 public int getScore() { return score; } // 重写toString方法 @Override public String toString() { return "Student[name=" + name + ", score=" + score + "]"; } } public class Main { public static void main(String[] args) { // 使用无参构造方法创建对象 Student student1 = new Student(); student1.setName("tom"); student1.setScore(90); // 使用有参构造方法创建对象 Student student2 = new Student("jack", 89); // 输出对象信息 System.out.println(student1); // 输出:Student[name=tom, score=90] System.out.println(student2); // 输出:Student[name=jack, score=89] } }