Discuss / Java / 七柒

七柒

Topic source

public class Test{

    public static void main(String[] args){

        Income[] incomes = new Income[]{

                new Income(8500),

                new Salary(4500),

                new Gaofei(500),

                new SpecialIncome(5000),

        };

        System.out.println(totalTax(incomes));

    }

    public static double totalTax(Income... incomes){

        double total = 0;

        for(Income t: incomes){

            total += t.getTax();

        }

        return total;

    }

}

class Income{

    double income;

    public Income(double income){

        this.income = income;

    }

    public double getTax(){

        return income*0.1;//工资税

    }

}

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(){

        return income*0.1;

    }

}

class SpecialIncome extends Income{

    public SpecialIncome(double income){

        super(income);

    }

    @Override

    public double getTax(){

        return 0;

    }

}


  • 1

Reply