Java继承的简单实现
程序员文章站
2022-03-10 16:09:25
Java继承的简单实现package inheritance;//继承public class ManagerTest {public static void main(String[] args) {// TODO Auto-generated method stubManager boss = new Manager("Carl Cracker",80000,1987,12,15);boss.setBonus(5000);Employee[] staff=ne...
Java继承的简单实现
package inheritance;
//继承
public class ManagerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Manager boss = new Manager("Carl Cracker",80000,1987,12,15);
boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee("Harry Hacker",50000,1989,10,1);
staff[2]=new Employee("tommy Tester",40000,1990,3,15);
for(Employee e:staff){
System.out.println("name="+e.getName()+",salary="+e.getSalary()+" "+e.getHireDay());
}
}
}
员工类
package inheritance;
import java.time.*;
public class Employee {
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String name,double salary,int year,int month,int day){
this.name=name;
this.salary=salary;
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;
}
}
经理类继承员工类并添加特有属性
package inheritance;
public class Manager extends Employee {
private double bonus;
public Manager(String name,double salary,int year,int month,int day){
super(name,salary,year,month,day);
bonus=0;
}
public double getSalary(){
double baseSalary=super.getSalary();
return baseSalary+bonus;
}
public void setBonus(double b){
bonus=b;
}
}
本文地址:https://blog.csdn.net/m0_49548571/article/details/109966767