【案例4-6】研究生薪资管理
在学校中,学生每个月需要交相应的生活费,老师每个月有相应的工资,而在职研究生既是老师又在职研究生既需要交学费又会有工资。下面要求编写一个程序来统计在职研究生的收入和学费,如果收入减去学费不足2000元,则输出" provide a loan "(需要贷款)信息。
用接口实现
【实现代码】
/**
* ZAY 2023.7.26
*/
import java.util.Scanner;
public class Example14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
YanJiuSheng student = new YanJiuSheng();
student.cost();
int x = sc.nextInt();
student.sal();
int y = sc.nextInt();
int z = y - x;
if (z < 2000 ) {
System.out.println("privade a loan");
} else {
System.out.println("no problem");
}
}
}
class YanJiuSheng implements Teacher, Student{
@Override
public void cost() {
System.out.println("学费为:");
}
@Override
public void sal() {
System.out.println("收入为:");
}
}
interface Teacher {
void sal();
}
interface Student {
void cost();
}
【运行截图】