/** * @ author : tangxi.zq * @ time : 2019-06-14 * @ Description : 多态 */ class Polymorphism { public static void main(String[]args) { Income[] incomes = new Income[] {new Income(3000), new SalaryIncome(7500), new RoyaltyIncome(12000)}; double total = 0; for (Income in :incomes) { total += in.getTax(); } System.out.println("总税费为:" + total); } } /** * 稿费收入税率是20% */ class RoyaltyIncome extends Income { // TODO public RoyaltyIncome(double income) { super(income); } @Override public double getTax() { return this.income * 0.2; } } 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 Income { protected double income; public Income(double income) { this.income = income; } public double getTax() { return income * 0.1; // 税率10% } }
Sign in to make a reply
用户5527594890