📄 innerinterface.java
字号:
/*【例4-24】 定义一个“部门”外部类Department,然后在其中定义
* “内部接口”InOut以定制输入输出服务,“员工”成员类Employee实
* 现了接口InOut,成员类Employee能方便地访问其外部类Department
* 的“部门、员工号”信息,并对工号自动编号。
*/
//程序清单4-24: InnerInterface.java
package e4_24;
import java.util.*;
public class InnerInterface {// 主类
public static void main(String[] args) {
Department department = new Department("计算机系");// 创建部门实例
// 为该部门创建第一名员工
Department.Employee employee1 = department.new Employee();
employee1.input();// 输入员工信息
employee1.output();// 输出员工信息
// 为该部门创建第二名员工
Department.Employee employee2 = department.new Employee();
employee2.input();// 输入员工信息
employee2.output();// 输出员工信息
}
}
class Department {// 外部类Department
private String eDeparment;// 部门成员变量
private static int eCount = 0;// 部门员工编号器
public Department(String department) {// 外部类构造方法
eDeparment = department;
}
interface InOut {// 内部接口InOut,定制输入输出服务
void input();
void output();
}
class Employee implements InOut {// 成员类Employee,并实现内部接口
private String eID;// 工号
private String eName;// 姓名
private double ePay;// 工资
public void input() {// 实现输入员工信息方法input()
eID = new Integer(++eCount).toString();
Scanner sin = new Scanner(System.in);
System.out.print("请输入姓名:");
eName = sin.next();
System.out.print("请输入工资:");
ePay = sin.nextDouble();
}
public void output() {// 实现输出员工信息方法onput()
System.out.println("工号:" + eID + "\t姓名:" + eName + "\t部门:"
+ eDeparment + "\t工资:" + ePay);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -