Discuss / Java / 画瓢

画瓢

Topic source

Im袁国伟

#1 Created at ... [Delete] [Delete and Lock User]
public class Main {
    public static void main(String[] args){        Income[] incomes = new Income[]{            new Income(0),            new Salary(6000),            new Contribution(4500)        };        System.out.println(totalTax(incomes));    }    public static double totalTax(Income...incomes){        double total = 0;        for (Income income: incomes){            total = total + income.getTax();        }        return total;    }    static class Income{        protected double income;        public Income(double income){            this.income = income;        }        public double getTax() {            return income * 0.1;        }    }    static class Salary extends Income{        public Salary(double income){            super(income);        }        @Override        public double getTax(){            if(income <= 5000){                return 0;            }            return (income - 5000) * 0.2;        }    }    static class Contribution extends Income{        public Contribution(double income){            super(income);        }        @Override        public double getTax(){            if(income <= 4000){                return (income - 800)*0.2*(1-0.3);            }            return (income * 0.8)*0.2*(1-0.3);        }    }}

  • 1

Reply