java编程题,求java高手,可执行代码

2025-01-04 15:26:59
推荐回答(1个)
回答1:

//Teacher.java
public class Teacher {
//4个String类型的私有字段Name、TeacherID、Address、Course
private String Name;
private String TeacherID;
private String Address;
private String Course;

public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getTeacherID() {
return TeacherID;
}
public void setTeacherID(String teacherID) {
TeacherID = teacherID;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getCourse() {
return Course;
}
public void setCourse(String course) {
Course = course;
}
//查询老师授课课程的方法queryCourse
public String queryCourse(String qryString){
if(qryString.equals(TeacherID)){
return Course;
}
return "";
}
}

//Test.java
public class Test {
public static void main(String[] args) {
Teacher t = new Teacher();
//设置该对象中的4个私有字段分别为:TeacherID=”001”、Name=”Peter”、Address=”Beijing”、Course=”OS”
t.setTeacherID("001");
t.setName("Peter");
t.setAddress("Beijing");
t.setCourse("OS");
String qryString = "001";
//显示“Teacher:此处为对象的Name,Course:此处为对象的课程”
System.out.println("Teacher:"+t.getName()+",Course:"+t.queryCourse(qryString));
}
}