类----类的封装测试,构造方法,toString()重写

class Employee {
    private int emp_id;
    private String emp_name;
    private float emp_salary;

    public Employee(int emp_id, String emp_name, float emp_salary) {
        this.emp_id = emp_id;
        this.emp_name = emp_name;
        this.emp_salary = emp_salary;
    }

    public String toString() {
        String str_rs = "员工ID:\t" + this.emp_id;
        str_rs += "\r\n员工姓名:\t" + this.emp_name;
        str_rs += "\r\n员工薪水:\t" + this.emp_salary +" 元";
        return str_rs;
    }

    public float addSalary(float f_salary){
        System.out.println("恭喜员工 "+this.emp_name+" 涨了 "+ f_salary + " 元工资!");
        return this.emp_salary += f_salary;
    }

    public float subSalary(float f_salary){
        System.out.println("悲催员工 "+this.emp_name+" 因业务能力差,下调了 "+ f_salary + " 元工资!");
        return this.emp_salary -= f_salary;
    }

    public int getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }

    public String getEmp_name() {
        return emp_name;
    }

    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }

    public float getEmp_salary() {
        return emp_salary;
    }

    public void setEmp_salary(float emp_salary) {
        this.emp_salary = emp_salary;
    }
}



public class ClassTest {
    public static void main(String[] args) {
        //面向对象 类的封装测试一

        Employee emp_cts = new Employee(1,"程天爽",5000.00f);
        Employee emp_zxm = new Employee(2,"张晓敏",3500.00f);
        emp_cts.addSalary(3000.00f);
        emp_zxm.subSalary(300.00f);
        System.out.println(emp_cts);
        System.out.println(emp_zxm);
        
    }
}

output:

恭喜员工 程天爽 涨了 3000.0 元工资!
悲催员工 张晓敏 因业务能力差,下调了 300.0 元工资!
员工ID: 1
员工姓名: 程天爽
员工薪水: 8000.0 元
员工ID: 2
员工姓名: 张晓敏
员工薪水: 3200.0 元

posted @ 2014-02-20 14:11  小菜喵  阅读(478)  评论(0编辑  收藏  举报