九州体育网 - 十年信誉网站

Discuss / Java / 练习打卡!

练习打卡!

Topic source

YANGZY1202

#1 Created at ... [] []
//Main.java
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 = 0;
		for (Income income : incomes) {
			total += income.getTax();
		}
		System.out.println(total);
	}

}

//Income.java

public class Income {

	protected double income;

	public Income(double income) {
		this.income = income;
	}

	public double getTax() {
		return income * 0.1; // 税率10%
	}

}

//SalaryIncome.java

public class SalaryIncome extends Income {

	public SalaryIncome(double income) {
		super(income);
	}

	@Override
	public double getTax() {
		if (income <= 5000) {
			return 0;
		}
		return (income - 5000) * 0.2;
	}
}

//RoyaltyIncome.java

public class RoyaltyIncome extends Income{

	public RoyaltyIncome(double income) {
		super(income);
	}

	@Override
	public double getTax() {
		
		return income * 0.2;  //稿费收入税率是20%
	}

}



  • 1

Reply