📄 abstractinnerclass.java
字号:
/*【例4-25】 定义一个“部门”外部类Department,然后在其中定义
* 员工“抽象内部类”AbstractEmployee,以定制其属性和输入输出服务,
* “员工”成员类Employee继承了抽象类AbstractEmployee,成员类Employee
* 能方便地访问其外部类Department的“部门、员工号”信息,并对工号自动编号。
*/
//程序清单4-25: AbstractInnerClass.java
package e4_25;
import java.util.Scanner;
public class AbstractInnerClass {
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 eDepartment) {// 外部类构造方法
this.eDeparment = eDepartment;
}
// 内部抽象类AbstractEmployee,定制输入输出服务
abstract class AbstractEmployee {
String eID;// 工号
String eName;// 姓名
double ePay;// 工资
abstract void input();
abstract void output();
}
// 实例成员类Employee,并继承内部抽象类bstractEmployee
class Employee extends AbstractEmployee {
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 + -