public class TestDemo{
public static void main(String args[]){
Emp emp1 =new Emp(001,"tom","CEO",300000.0,5000.0); //实例化时调用构造方法,利用构造方法传递参数,避免使用setter方法传参
System.out.println(emp1.tell()) ; //匿名对象代码实现如下 new Emp(001,"王强","开发工程师",300000,5000).tell();匿名对象只能使用一次就会变成垃圾
}
}
class Emp{
private int empno; //属性
private String ename;
private String job;
private double sal;
private double comm;
public Emp(){ //构造方法
}
public Emp(String n){
setEname(n);
}
public Emp(int e,String n,String j,double s,double c){
setEmpno(e);
setEname(n);
setJob(j);
setSal(s);
setComm(c);
}
public void setEmpno(int e){ //普通方法
empno=e;
}
public void setEname(String n){
ename=n;
}
public void setJob(String j){
job=j;
}
public void setSal(double s){
sal=s;
}
public void setComm(double c){
comm=c;
}
public int getEmpno(){
return empno;
}
public String getEname(){
return ename;
}
public String getJob(){
return job;
}
public double getSal(){
return sal;
}
public double getComm(){
return comm;
}
public String tell(){
return "雇员编号:"+empno+"\n"+
"雇员姓名:"+ename+"\n"+
"雇员职位:"+job+"\n"+
"雇员工资:"+sal+"\n"+
"雇员奖金:"+comm;
}
}