package com.baidu.demo06;
import java.util.Scanner;
//所有员工的父类
public class Employee {
private int monthN = 6;//假设现在六月
private int month;
private int salary = 100;
public Employee() {
setMonth(month);
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getSalary() {
if (monthN == month) {
return salary;
} else return 0;
}
}
package com.baidu.demo06;
public class SalariedEmployee extends Employee {
private double salary;
public SalariedEmployee(double salary) {
// TODO Auto-generated constructor stub
super();
setSalary(salary);
}
@Override
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package com.baidu.demo06;
public class HourlyEmployee extends Employee {
private double hourlySalary;
private int hour;
public HourlyEmployee(double hourlySalary, int hour) {
super();
setHour(hour);
setHourlySalary(hourlySalary);
}
@Override
public double getSalary() {
if (hour > 160)
return 160 * hourlySalary + (hour - 160) * 1.5 * hourlySalary;
else
return hour * hourlySalary;
}
public double getHourlySalary() {
return hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
}
package com.baidu.demo06;
public class SalesEmployee extends Employee {
private double sale;
private double addRate;
public SalesEmployee(double sale, double addRate) {
setAddRate(addRate);
setSale(sale);
}
@Override
public double getSalary() {
// TODO Auto-generated method stub
return sale * addRate;
}
public double getSale() {
return sale;
}
public void setSale(double sale) {
this.sale = sale;
}
public double getAddRate() {
return addRate;
}
public void setAddRate(double addRate) {
this.addRate = addRate;
}
}
package com.baidu.demo06;
public class BasePlusSaleEmployee extends SalesEmployee {
private double baseSalary;
private double addRate;
private double sale;
public BasePlusSaleEmployee(double sale, double addRate, double baseSalary) {
super(sale, addRate);
// TODO Auto-generated constructor stub
setAddRate(addRate);
setBaseSalary(baseSalary);
setSale(sale);
}
@Override
public double getSalary() {
SalesEmployee a = new SalesEmployee(sale, addRate);
// TODO Auto-generated method stub
return baseSalary + a.getSalary();
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getAddRate() {
return addRate;
}
public void setAddRate(double addRate) {
this.addRate = addRate;
}
public double getSale() {
return sale;
}
public void setSale(double sale) {
this.sale = sale;
}
}
package com.baidu.demo06;
import java.util.Calendar;
public class Text {
public static void main(String[] args) {
Employee employee[] = {
new SalariedEmployee(1000),
new HourlyEmployee(16, 150),
new SalesEmployee(500, 0.2),
new BasePlusSaleEmployee(500, 0.2, 1000)
};
Calendar cal = Calendar.getInstance();
cal.set(2018, 6, 18);
for (int i = 0; i < employee.length; i++) {
int month = cal.get(Calendar.MONTH);
Employee a = new Employee();
a.setMonth(month);
double welfare = a.getSalary();
double salary = employee[i].getSalary();
System.out.println("第" + (i + 1) + "个:" + (welfare + salary));
}
}
}
封装继承多态练习
最新推荐文章于 2024-11-14 09:21:17 发布