类----类中可以有多个构造方法

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

    Employee() {
    }

    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);
        Employee emp_zs = new Employee();
        emp_zs.setEmp_id(3);
        emp_zs.setEmp_name("张三");
        emp_zs.setEmp_salary(4330.5f);
        emp_zs.addSalary(530.0f);
        emp_zs.subSalary(300.2f);
        emp_cts.addSalary(3000.00f);
        emp_zxm.subSalary(300.00f);
        System.out.println(emp_cts);
        System.out.println(emp_zxm);
        System.out.println(emp_zs);
        
    }
}

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

转载于:https://www.cnblogs.com/xiaocaimiao/p/3557693.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值