Java - 子类重写父类的方法

  如果子类可以继承父类的某个方法,子类就可以重写这个方法 . 比如实例: 

测试类:

public class Test3_17 {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double english = 73.6,math = 65  ,chinese = 67;
		LargeCompany a = new LargeCompany() ; 
		a.applyRule(math, english, chinese);
	}

}

父类: 

public class Company {
	
	public void applyRule(double math , double english ,double chinese) {
		double total = math + english + chinese ; 
		if(total >200) {
			System.out.println("总成绩为 : " + total +"达到了公司的要求");
		}
		else {
			System.out.println("总成绩为 : " + total +"未达到了公司的要求");
		}
	}
}

子类 :

public class LargeCompany extends Company{
	// 重写父类的方法
	public void applyRule(double math ,double english , double chinese ) {
		double total = math + english + chinese ; 
		if(total >260) { // 改了这里 
			System.out.println("总成绩为 : " + total +"达到了公司的要求");
		}
		else {
			System.out.println("总成绩为 : " + total +"未达到了公司的要求");
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/88601685