gistfile1.txt package user;//定义部门类class Dept{private int deptno;private String dname;private String loc;private Emp emps[];public Dept() {super();// TODO Auto-generated constructor stub}public Dept(int deptno, String dname, String
package user;
//定义部门类
class Dept{
private int deptno;
private String dname;
private String loc;
private Emp emps[];
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(int deptno, String dname, String loc, Emp[] emps) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
this.emps = emps;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Emp[] getEmps() {
return emps;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public String getInfo(){
return "部门编号" + this.deptno
+ "名称" + this.dname
+ "职位" + this.loc;
}
}
//定义员工类
class Emp {
private int empno; //编号
private String ename; //名称
private String job; //工作职位
private double sal; //工资
private double comm; //佣金
private Dept dept; //对应的部门
private Emp mgr; //对应的领导类
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int empno, String ename, String job, double sal, double comm) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp getMgr() {
return mgr;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public String getInfo(){
return "编号" + this.empno
+ "名称" + this.ename
+ "职位" + this.job
+ "工资" + this.sal
+ "佣金" + this.comm;
}
}
public class Other {
public static void main(String[] args) {
Dept dept = new Dept(10,"ACCOUNTING","New York", null);
Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0) ; // 雇员信息
Emp eb = new Emp(7902,"FORD","MANAGER",2450.0,0.0) ; // 雇员信息
Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0) ; // 雇员信息
// 2.设置雇员和领导关系
ea.setMgr(eb) ; // 设置雇员领导
eb.setMgr(ec) ; // 设置雇员领导
// 3.设置雇员和部门关系
ea.setDept(dept) ; // 雇员与部门
eb.setDept(dept) ; // 雇员与部门
ec.setDept(dept) ; // 雇员与部门
dept.setEmps( new Emp[]{ea,eb,ec}) ; // 部门与雇员
System.out.println(ea.getInfo());
// 第二步:根据表结构描述取得设置的数据
System. out .println(ea.getInfo()) ; // 取得雇员信息
System. out .println("\t|- " + ea.getMgr().getInfo()) ; // 取得雇员领导信息
System. out .println("\t|- " + ea.getDept().getInfo()) ; // 取得雇员部门信息
// 取得部门的完整信息,包括部门基础信息以及部门中的所有员工和每个员工的领导信息
System. out .println(dept.getInfo()) ; // 部门信息
for ( int x = 0 ; x < dept.getEmps().length ; x ++) { // 所有雇员信息
System. out .println("\t|- " + dept.getEmps()[x].getInfo()) ; // 雇员信息
if (dept.getEmps()[x].getMgr() != null) { // 判断是否存在领导信息
System. out .println("\t\t|- " +
dept.getEmps()[x].getMgr().getInfo()); // 领导信息
}
}
}
}
