一一一 实验内 2.学校中有教师和学生两类人,而在职博士生既是教师又是学生。设计两个 接口StuInterface和TeaInterface。其中,StuInterface接口包括对学费的set()和get)方法,分别用于设置和获取学生的学费;TeaInterface接容。口包括对工资的set()和get()方法,分别用于设置和获取教师的工资。。 定义一个博士生类Doctor,实现StuInterface接口和TeaInterface接口,它的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、pay(月工资),如果收入减去学费不足3000元,则输出“provide a loan”(需要贷款)信息。编写测试类,测试所创建的Doctor类。
时间: 2024-01-24 07:19:14 浏览: 25
以下是Java代码实现:
```
// StuInterface接口
interface StuInterface {
void setFee(double fee); // 设置学费
double getFee(); // 获取学费
}
// TeaInterface接口
interface TeaInterface {
void setPay(double pay); // 设置工资
double getPay(); // 获取工资
}
// Doctor类实现StuInterface和TeaInterface接口
class Doctor implements StuInterface, TeaInterface {
private String name;
private String sex;
private int age;
private double fee; // 学费
private double pay; // 工资
public Doctor(String name, String sex, int age, double fee, double pay) {
this.name = name;
this.sex = sex;
this.age = age;
this.fee = fee;
this.pay = pay;
}
// 实现StuInterface接口方法
public void setFee(double fee) {
this.fee = fee;
}
public double getFee() {
return fee;
}
// 实现TeaInterface接口方法
public void setPay(double pay) {
this.pay = pay;
}
public double getPay() {
return pay;
}
// 判断是否需要贷款
public void checkLoan() {
if (pay - fee < 3000) {
System.out.println("provide a loan");
}
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Doctor doctor = new Doctor("张三", "男", 30, 5000, 6000);
System.out.println(doctor.getFee()); // 输出5000.0
System.out.println(doctor.getPay()); // 输出6000.0
doctor.setFee(6000);
doctor.setPay(5000);
System.out.println(doctor.getFee()); // 输出6000.0
System.out.println(doctor.getPay()); // 输出5000.0
doctor.checkLoan(); // 输出provide a loan
}
}
```