Discuss / Java / 叮
public class Main {
    public static void main(String[] args) {
        // 给一个有工资收入和稿费收入的小伙伴算税:
        Income[] incomes = new Income[] {
            new Salary(8000),
            new Gaofei(20000)
        };
        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 {
    protected double income;

    public Income(double income) {
        this.income = income;
        System.out.println(income);
    }

    public double getTax() {
        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 Gaofei extends Income{
    public Gaofei(double income){
        super(income);
    }
    
    @Override
    public double getTax(){
        if(income < 4000){
           return (income-800) * 0.7 * 0.2;
        }
        return income * (1 - 0.2) * 0.7 *0.2;
    }
}

  • 1

Reply