// 多态
/*
* 如果方法签名不同,就是Overload
* 方法签名相同,并且返回值也相同,就是Override
* 标志重写父类方法*/
public class Object1 {
public static void main(String[] args) {
// 给一个有普通收入、工资收入和享受国务院特殊津贴的小伙伴算税:
Income[] incomes = new Income[] {
new Income(3000),
new Salary(7500),
new StateCouncilSpecialAllowance(15000)
};
System.out.println(totalTax(incomes));
}
public static double totalTax(Income... incomes) {
double total = 0;
for (Income income: incomes) {
total = total + income.getTax();
}
return total;
}
}
class Income {
//final class Income { // final作用是:该类不能够被继承
protected double income;
//public final String name="小帅"; // final作用是:该字段初始化之后不能够被赋值
public Income(double income) {
this.income = income;
}
public double getTax() {
//public final double getTax() { // final作用是该父类方法不能被重写
return income * 0.1; // 税率10%
}
}
class Salary extends Income {
public Salary(double income) {
super(income);
}
@Override
public double getTax() {
if (income <= 5000) {
return 0;
}
return (income - 5000) * 0.2;
}
}
class StateCouncilSpecialAllowance extends Income {
public StateCouncilSpecialAllowance(double income) {
super(income);
}
@Override
public double getTax() {
return 0;
}
}