Discuss / Java / 练习

练习

Topic source
public class Main{
	public static void main(String[] args) {
		//给一个有工资收入和稿费收入的小伙伴算税。
		Salary[] incomes = new Salary[] {
				new Salary(7000),
				new Fee(5000)
		};
		//输出结果
		System.out.println(totalTax(incomes));//总共需要缴纳的税费为900.0
	}
	//计算总共需要缴纳的税费
	public static double totalTax(Salary...incomes) {
		double total = 0;
		for(Salary income : incomes) {
			total += income.getTax();
		}
		return total;
	}
}

class Salary {
	protected double salary ;
	
	
	//初始化工资收入
	public Salary (double salary ) {
		this.salary  = salary;
	}
	
	//计算工资收入所需要交的税0.2
	public double getTax() {
		if(salary <= 5000 ) {
			return 0;
		}
		return (salary-5000)*0.2;
	}
	
	
}

class Fee extends Salary{
	protected double fee;
	
	//初始化稿费收入
	public Fee(double fee) {
		super(fee);
		this.fee = fee;
	}
	
	//计算稿费收入所需要交的税0.1
	@Override
	public double getTax() {
		return fee*0.1;
	}
	
}

  • 1

Reply