类----类中可以有多个构造方法
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 元