Employee类,打印员工的工资,将工资提高5%

import java.time.*;
public class EmloyeeTest {
    public static void main(String[] args) {
        Emloyee[] staff=new Emloyee[3];
        staff[0]=new Emloyee("Tony",75000,2018,5,4);
        staff[1]=new Emloyee("Harry",50000,2018,8,4);
        staff[2]=new Emloyee("Carl",40000,2018,3,3);
        for(Emloyee e:staff)//for each语句增强循环,效果与for(e=0;e<staff.length;e++)一样的,都是打印数组里的每一个元素
            e.raiseSalary(5);
        for(Emloyee e:staff)
            System.out.println("name="+e.getName()+",Salary="+e.getSalary()+",hireDay="+e.getHireDay());
    }
}
class Emloyee{
    private String name;
    private double salary;
    private LocalDate hireDay;
    public Emloyee(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+=raise;
    }
}

猜你喜欢

转载自blog.csdn.net/hml666888/article/details/81408421