Discuss / Java / 用抽象类算税

用抽象类算税

Topic source
public abstract class Shui {
	protected double shui;
	public Shui(double shui) {
		this.shui = shui;
	};
	public abstract double getTax();
}

class Salary extends Shui{
	public Salary(double shui) {
		super(shui);
	}
	public double getTax() {
		if(this.shui <= 5000) {
			return 0;
		}
		else {
			return(this.shui - 5000) * 0.2;
		}
	}
}

class GaoFei extends Shui{
	public GaoFei(double shui) {
		super(shui);
	}
	public double getTax() {
		return this.shui * 0.1;
	}
}

class Text{
	public static void main(String[] args) {
		Shui[] shui = new Shui[] {new Salary(6000),new GaoFei(8000)};
		double m = 0;
		for (Shui sh:shui) {
			m += sh.getTax();
		}
		System.out.print(m);
	}
}

  • 1

Reply