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;
    }

}

interface  Income{
	double getTax();
}

//薪资计算
class Salary implements Income{
	
	private double income;
	public Salary(){}
	
	public Salary(double income) {
			this.income = income;
	}
	
	public double getTax() {
		if(this.income < 5000) {
			return 0;
		}
		return (this.income - 5000)*0.2;
	}
}

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


  • 1

Reply