1. 程式人生 > >Employee類

Employee類

public emp int err tostring setname ble pac args

 1 package demo;
 2 
 3 import java.time.LocalDate;
 4 
 5 public class Employee {
 6     private String name;
 7     private double salary;
 8     private LocalDate hireDay;
 9 
10     public Employee(String n,double s, int year,int month,int day) {
11         this.name = n;
12         this.salary = s;
13 this.hireDay = LocalDate.of(year,month,day); 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public double getSalary() { 25 return salary; 26 } 27 28 public
void setSalary(double salary) { 29 this.salary = salary; 30 } 31 32 public LocalDate getHireDay() { 33 return hireDay; 34 } 35 36 public void setHireDay(LocalDate hireDay) { 37 this.hireDay = hireDay; 38 } 39 40 @Override 41 public String toString() {
42 return "Employee{" + 43 "name=‘" + name + ‘\‘‘ + 44 ", salary=" + salary + 45 ", hireDay=" + hireDay + 46 ‘}‘; 47 } 48 49 public void raiseSalary(double byPercent){ //增加薪水函數 50 double raise=salary*byPercent/100; 51 salary+=raise; 52 } 53 }
 1 package demo;
 2 
 3 public class EmployeeTest {
 4     public static void main(String[] args) {
 5         Employee[] staff=new Employee[4];
 6 
 7         staff[0]=new Employee("迪迦",50000,2001,11,11);
 8         staff[1]=new Employee("戴拿",60000,2006,1,1);
 9         staff[2]=new Employee("銀河",40000,2106,6,7);
10         staff[3]=new Employee("羅布",53000,2018,7,1);
11 
12         for (Employee e:staff) {    // 增加每個人5%的薪水
13             e.raiseSalary(5);
14         }
15 
16             for (Employee f:staff){    //以表格的形式把員工表輸出出來
17                 System.out.println(f.toString());
18             }
19         }
20 }

Employee類