Discuss / Java / 算税:

算税:

Topic source

/**

 * Learn Java from https://www.liaoxuefeng.com/

 * 

 * @author liaoxuefeng

 */

public class Main {

public static void main(String[] args) {

// TODO: 给一个有工资收入和稿费收入的小伙伴算税:

Income[] incomes = new Income[] { new Income(3000), new SalaryIncome(7500), new RoyaltyIncome(12000) };

double total = getIncome(incomes );

// TODO:

System.out.println(total);

}

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

double money = 0.0;

for (Income income : incomes) {

money+=income.getTax();

}

return money;

}

}

class RoyaltyIncome extends Income{

public RoyaltyIncome(double income) {

super(income);

// TODO Auto-generated constructor stub

}

public double getTax() {

return 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;//稿费税率20%

}

}

class Income {

protected double income;

public Income(double income) {

this.income = income;

}

public double getTax() {

return income * 0.1; // 税率10%

}

}


  • 1

Reply