Java一个简单的Employee类

import java.time.LocalDate;

public class EmployeeTest {
    public static void main(String[] args)  {
        Employee[] staff = new Employee[3];     //构造一个Employee数组并填入了三个雇员对象信息

        staff[0] = new Employee("Carl Cracker",50000, 1989, 10, 1);
        staff[1] = new Employee("Harry Hacker",50000, 1989, 10, 1);
        staff[2] = new Employee("Tony Tester",40000, 1990, 3, 15);

        for (Employee e : staff)    //利用Employee类的raiseSalary方法将每个雇员的薪水提高5%
            e.raiseSalary(5);

        for (Employee e : staff)    //调用getName、getSalary和getHireDay方法将每个雇员的信息打印出来
            System.out.println("name="+e.getName()+",salary="+e.getSalary()+",hireDay="+e.getHireDay());
    }
}

class Employee{
    private String name;
    private double salary;
    private LocalDate hireDay;

    public Employee(String n, double s, int year, int month, int day){     //一个雇员信息构造器
        name = n;
        salary = s;
        hireDay = LocalDate.of(year, month, day);
    }

    public String getName(){    //返回姓名值的方法
        return name;
    }

    public double getSalary(){      //返回薪水值的方法
        return salary;
    }

    public LocalDate getHireDay(){      //返回入职日期方法
        return hireDay;
    }

    public void raiseSalary(double byPercent){      //提高薪水的方法
        double raise = salary * byPercent / 100;
        salary = salary + raise;
    }
}

你可能感兴趣的:(java)