Java的重寫與呼叫父類方法問題
阿新 • • 發佈:2019-02-11
一. Java重寫父類方法
class Father{
public void tostring() {
System.out.println("Hello!");
}
}
class Sun extends Father{
public void tostring(){
System.out.println("xdlove!");
}
public void Super_tostring() {
super.tostring();
}
}
二. 當我們重寫了父類方法後,又想同時呼叫父類的方法,而不是已經重寫了的方法,可以這樣
public class main{ static Scanner cin = new Scanner(System.in); public static void main(String[] args) { Sun sun = new Sun(); sun.tostring(); sun.Super_tostring(); } } class Father{ public void tostring() { System.out.println("Hello!"); } } class Sun extends Father{ public void tostring(){ System.out.println("xdlove!"); } public void Super_tostring() { //採用super來呼叫父類的方法 super.tostring(); } }