Discuss / Java / 练习

练习

Topic source

Cecilia

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

      // 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 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 class RoyaltyIncome extends Income{   // TODO    public RoyaltyIncome(double income){        super(income);    }    public double getTax(){        return income*0.2;    }}
public class SalaryIncome extends Income{   // TODO    public SalaryIncome(double income){        super(income);    }    public double getTax(){        if (income<5000){            return 0;        }        return (income-5000)*0.2;    }}

  • 1

Reply