編寫一個程式,Manager類繼承Employee類,輸出員工和經理的資訊。
編寫一個程式,要求設定一個Employee 類和一個Manager類,經理類繼承僱員類的獲取名字、工資、日期方法,經理類新增一個獲取獎金的方法,經理的工資為獎金與基本薪水的總額,打印出所有僱員和經歷的名字,工資,日期。
package inheritance;
import java.time.*;
public class ManagerTest {
public static void main(String[] args) {
Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
boss.setBouns(5000);
Employee[]
staff[0]=boss;
staff[1]=new Employee("Harry",50000,1989,10,1);
staff[2]=new Employee("Tommy",40000,1990,3,15);
for(Employee e:staff) System.out.println("name="+e.getName()+",salary="+e.getSalary()+",hireDay="+e.getHireDay());
}
}
public class Employee {
private String name;
private
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 percent){
double raise=salary*percent/100;
salary+=raise;
}
}
public class Manager extends Employee{
private double bouns;
public Manager(String n,double s,int year,int month,int day){
super(n,s,year,month,day);
bouns=0;
}
public double getSalary(){
double baseSalary=super.getSalary();
return baseSalary+bouns;
}
public void setBouns(double b){
bouns=b;
}
}
輸出結果: