我们需要设计两个接口 StuInterface 和 TeaInterface,分别用于设置和获取学生的学费以及教师的工资。然后定义一个博士生类 Doctor,实现这两个接口。最后编写测试类
代码如下: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;
@Override
public void setFee(double fee) {
this.fee = fee;
}
@Override
public double getFee() {
return fee;
}
@Override
public void setPay(double pay) {
this.pay = pay;
}
@Override
public double getPay() {
return pay;
}
public void checkIncome() {
if (pay - fee < 3000) {
System.out.println("provide a loan");
}
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Doctor doctor = new Doctor();
doctor.setFee(5000);
doctor.setPay(8000);
System.out.println("学费:" + doctor.getFee());
System.out.println("工资:" + doctor.getPay());
doctor.checkIncome();
}
}
解析:
1. 定义接口 StuInterface,包含 setFee() 和 getFee() 方法;
2. 定义接口 TeaInterface,包含 setPay() 和 getPay() 方法;
3. 定义博士生类 Doctor,实现 StuInterface 和 TeaInterface 接口;
4. 在 Doctor 类中,添加成员变量 name、sex、age、fee、pay;
5. 在 Doctor 类中,实现接口中的方法;
6. 判断收入减去学费是否不足3000元,如果不足则输出 "provide a loan";
7. 编写测试类,测试 Doctor 类
运行测试类,输出结果如下:
学费:5000.0
工资:8000.0
provide a loan