完善下列代码:public class Student { private int id; private String name; private int classId; private int teacherId; // 构造函数、Getter和Setter方法 } public class Class { private int id; private String name; private int teacherId; // 构造函数、Getter和Setter方法 } public class Teacher { private int id; private String name; // 构造函数、Getter和Setter方法 }
时间: 2024-03-03 18:54:02 浏览: 82
学生类的构造函数
下面是完整的代码:
```java
public class Student {
private int id;
private String name;
private int classId;
private int teacherId;
public Student(int id, String name, int classId, int teacherId) {
this.id = id;
this.name = name;
this.classId = classId;
this.teacherId = teacherId;
}
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 getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
}
public class Class {
private int id;
private String name;
private int teacherId;
public Class(int id, String name, int teacherId) {
this.id = id;
this.name = name;
this.teacherId = teacherId;
}
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 getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
}
public class Teacher {
private int id;
private String name;
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
```
在每个类中,我们定义了构造函数和Getter/Setter方法来设置和获取私有变量。这些方法使得我们可以封装这些类并从其他类中访问它们的属性。
阅读全文