日期:2014-05-20  浏览次数:20694 次

怎么写一个打印员工信息的方法
SalariedEmployee e1=new SalariedEmployee("zhangsan",7);
e1.setMonthSalary(3000.0);
e1.getYearSalary(month);
System.out.println("员工的姓名是:"+e1.getName()+" 员工生日的月份是:"+e1.getBirthday()+" 员工的职员类型是:"+e1.getClass()+" 员工的工资是:"+e1.getYearSalary(month)+"元");

现在想写一个方法就是不是每次都写System.out.println("员工的姓名是:"+e1.getName()+" 员工生日的月份是:"+e1.getBirthday()+" 员工的职员类型是:"+e1.getClass()+" 员工的工资是:"+e1.getYearSalary(month)+"元");这句话而是通过调用一个打印的方法就可以打印员工的信息。
小弟初学java,求指教

多谢啦!


------解决方案--------------------
善用 toString()函数,在SalariedEmployee的类定义中:

public String toString() {
return "员工的姓名是:"+e1.getName()+" 员工生日的月份是:"+e1.getBirthday()+" 员工的职员类型是:"+e1.getClass()+" 员工的工资是:"+e1.getYearSalary(month)+"元";
}


以后使用就直接:
System.out.println(e1);
------解决方案--------------------
把e1去掉嘛。只留方法。
楼上的只是说给你这个思路。
我的异常网推荐解决方案:软件开发者薪资,http://www.myexception.cn/other/1391128.html