Discuss / Java / 练习

练习

Topic source

Cecilia

#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;        for (Income income: incomes){            total=total+income.getTax();        }        // TODO:        System.out.println(total);    }}
public interface Income {    // TODO    double getTax();}
public class RoyaltyIncome implements Income {    // TODO    protected double income;    public RoyaltyIncome(double income) {        this.income=income;    }    public double getTax(){        return income*0.2;    }}
public class SalaryIncome implements Income{    // TODO    protected double income;    public SalaryIncome(double income) {        this.income=income;    }    public double getTax(){        if (income<5000){            return 0;        }        return (income-5000)*0.1;    }}

  • 1

Reply