Discuss / Java / 作业打卡

作业打卡

Topic source
public class Main {

	public static void main(String[] args) {
		// TODO: 用抽象类给一个有工资收入和稿费收入的小伙伴算税:
		Income[] incomes = new Income[] {
				new BaseIncome(3000), 
				new SalaryIncome(7500), 
				new RoyaltyIncome(12000)
		};
		
		// TODO:
		double total = 0;
		for (Income income : incomes) {
			total += income.getTax();
		}
		System.out.println(total);
	}
}

/**
 * 定义抽象类Income
 */
public abstract class Income {

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

	public abstract double getTax();
}

public class BaseIncome extends Income{

	//TODO
	public BaseIncome(double income) {
		super(income);
	}
	
	@Override
	public double getTax() {
		return income * 0.1;
	}
}

public class SalaryIncome extends Income{

	//TODO
	public SalaryIncome(double income) {
		super(income);
	}
	
	@Override
	public double getTax() {
		if (income <= 5000) {
			return 0;
		}
		return income * 0.02;
	}
}

public class RoyaltyIncome extends Income {

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

	@Override
	public double getTax() {
		return income * 0.2;
	}
}


  • 1

Reply