对面向对象构造器的运用
创建部门类
package com.softeem.lesson08;
public class Dept {
/**
* 部门号
*/
private int dno;
/**
* 部门名称
*/
private String dname;
/**
* 分机号
*/
private String tel;
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
创建员工类
package com.softeem.lesson08;
public class Emp {
/**
* 员工工号
*/
private int eno;
/**
* 姓名
*/
private String ename;
/**
* 职位
*/
private String job;
/**
* 年龄
*/
private int age;
/**
* 月薪
*/
private double sal;
/**
* 员工所属部门:关联关系
*/
private Dept dept;
public int getEno() {
return eno;
}
public void setEno(int eno) {
this.eno = eno;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
测试类
package com.softeem.lesson08;
public class TestEmp {
public static void main(String[] args) {
Dept d = new Dept();
d.setDno(10);
d.setDname("研发部");
d.setTel("8009");
Emp e = new Emp();
e.setEno(7788);
e.setEname("张三");
e.setAge(20);
e.setJob("经理");
e.setSal(10000);
e.setDept(d);
Dept dept = e.getDept();
String dname = dept.getDname();// e.getDept().getDname()
System.out.println(e.getEname() + "是来自" + dname);
}
}