Discuss / Java / 作业,已验证。

作业,已验证。

Topic source

Nobody52297

#1 Created at ... [Delete] [Delete and Lock User]

public class Main {

public static void main(String[] args) {

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

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

double total = 0;

// TODO:

for(Income s:incomes) {

total += s.getTax();

}

System.out.println(total);

}

}

public abstract class Income {

double income;

public abstract double getTax();

}

public class SalaryIncome extends Income {

public SalaryIncome(double income) {

this.income = income;

}

@Override

public double getTax() {

return (income-5000)*0.2;

}

}

/**

 * 稿费收入税率是20%

 */

public class RoyaltyIncome extends Income {

public RoyaltyIncome(double income) {

this.income = income;

}

@Override

public double getTax() {

return income*0.2;

}

}

结果为2900.0


  • 1

Reply