public abstract class Income {
protected double income;
public Income(double income) {
this.income = income;
}
public abstract double getTax ();
// TODO
}
public class RoyaltyIncome extends Income {
public RoyaltyIncome (double income) {
super(income);
}
@Override
public double getTax() {
return income*0.2;
}
// TODO
}
public class SalaryIncome extends Income {
public SalaryIncome(double income) {
super(income);
}
@Override
public double getTax() {
double b = 0;
if (income<5000){
b = 0;
} else if(income>=5000) {
b= (income-5000)*0.2;
}
return b;
}
}
public class Main {
public static void main(String[] args) {
// TODO: 用抽象类给一个有工资收入和稿费收入的小伙伴算税:
Income[] incomes = new Income[] { new SalaryIncome(7500), new RoyaltyIncome(12000) };
double total = 0;
for (Income ic:
incomes) {
total = total +ic.getTax();
}
// TODO:
System.out.println(total);
}
}
龙眠野人张