public class Main { public static void main(String[] args) { // 给一个有工资收入和稿费收入的小伙伴算税 Income[] incomes = {new Income(3000), new SalaryIncome(7500), new RoyaltyIncome(12000)}; System.out.println(totalTax(incomes)); } /** * 计算总税费 * @param incomes * @return double */ public static double totalTax(Income[] incomes) { double total = 0; for (Income income : incomes) { total += income.getTax(); } return total; } } class Income { protected final double income; public Income(double income) { this.income = income; } public double getTax() { return income * 0.1; } } /** * 工资收入税率 */ class SalaryIncome extends Income { public SalaryIncome(double income) { super(income); } @Override public double getTax() { if (income <= 5000) { return 0; } return (income - 5000) * 0.2; } } /** * 稿费收入税率 */ class RoyaltyIncome extends Income { public RoyaltyIncome(double income) { super(income); } @Override public double getTax() { return income * 0.2; } }
Sign in to make a reply
韦雪松