Discuss / Java / 练习:

练习:

Topic source

ypx0410

#1 Created at ... [Delete] [Delete and Lock User]
public class Hello {
    public static void main(String[] args) {
    	Income[] incomes = new Income[] {
    			new Salary(20000),
    			new Royalties(10000)
    	};
    	
    	System.out.println(totalTax(incomes));
     }
    
    //main是静态方法,只能调用静态方法所以totalTax也为静态方法
    public static double totalTax(Income... incomes)
    {
    	double tax = 0;
    	for(Income income:incomes) {
    		tax += income.getTax();
    	}
    	return tax;
    }
}

abstract  class Income{
	protected double income;
	abstract public double getTax();
}

//薪资计算
class Salary extends Income{
	
	public Salary(){}
	
	public Salary(double income) {
			super.income = income;
	}
	
	@Override
	public double getTax() {
		if(super.income < 5000) {
			return 0;
		}
		return (super.income - 5000)*0.2;
	}
}

//稿费计算
class Royalties extends Income{
	public Royalties() {}
	
	public Royalties(double income) {
		super.income = income;
	}
	
	@Override
	public double getTax() 
	{
		//假设稿费交税为3成
		return super.income*0.3;
	}
}


  • 1

Reply