public class Main {
public static void main(String[] args) {
// 给一个有普通收入、写作收入的小伙伴算税:
Income[] incomes = new Income[] {
new Income(3000),
new Write(5000),
};
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;
public double getTax() {
return income * 0.1; // 税率10%
class Write extends Income {
public Write(double income) {
super(income);
@Override
if (income <= 5000) {
return 0;
return (income - 5000) * 0.2;
Sign in to make a reply
説025
public class Main {
public static void main(String[] args) {
// 给一个有普通收入、写作收入的小伙伴算税:
Income[] incomes = new Income[] {
new Income(3000),
new Write(5000),
};
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;
}
public double getTax() {
return income * 0.1; // 税率10%
}
}
class Write extends Income {
public Write(double income) {
super(income);
}
@Override
public double getTax() {
if (income <= 5000) {
return 0;
}
return (income - 5000) * 0.2;
}
}