实验1中的Student类代码: StudentTest主类:
时间: 2024-09-20 19:04:31 浏览: 32
Java语言程序设计A实验1:类与对象.pdf
由于实际的代码未给出,但我可以根据描述概括出其实验1可能的实现方式:
### Student 类代码示例
```java
public class Student {
private String name;
private int age;
private double grade;
private String id; // 可能的额外字段
private String email; // 如果有5个参数,则这是另一个额外字段
public Student(String name, int age, double grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
public Student(String name, int age, double grade, String id) {
this(name, age, grade); // 调用三个参数的构造器
this.id = id;
}
public Student(String name, int age, double grade, String id, String email) {
this(name, age, grade, id); // 调用四个参数的构造器
this.email = email;
}
// Getter 和 Setter 方法
public void setName(String name) { ... }
public String getName() { ... }
public void setAge(int age) { ... }
public int getAge() { ... }
public void setGrade(double grade) { ... }
public double getGrade() { ... }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
(id != null ? ", id='" + id + '\'' : "") +
(email != null ? ", email='" + email + '\'' : "") +
'}';
}
}
```
### StudentTest 主类代码示例
```java
public class StudentTest {
public static void main(String[] args) {
Student student1 = new Student("Alice", 20, 3.8);
System.out.println(student1);
Student student2 = new Student("Bob", 22, 3.6, "B123");
System.out.println(student2);
Student student3 = new Student("Charlie", 21, 3.9, "C456", "charlie@example.com");
System.out.println(student3);
}
}
```
请注意这只是一个基于题目描述的例子,并非确切的答案。具体实现时应考虑更多细节。
阅读全文