#Java#【1】子類呼叫父類被重寫的方法
阿新 • • 發佈:2018-11-15
一、程式碼
package com.atguigu.exer1; //========== Son =================== public class Son extends Father { public String str = "子類變數"; public void printf(String str){ System.out.println(str+"這是子類的方法"); } public void test(){ printf("什麼都不使用=======>");this.printf("使用this=======>"); super.printf("使用super=======>"); printfOnly("子類沒重寫,就會呼叫父類的方法"); System.out.println("str is ===========>"+str); System.out.println("super.str is ===========>"+super.str); System.out.println("子類沒有同名變數,就會去找父類的變數 ===========>"+strOnly); }public static void main(String[] args) { Son son = new Son(); son.test(); } } //========== Father =================== class Father{ public String str = "父類變數"; public String strOnly = "父類變數,子類沒有同名變數"; public void printf(String str){ System.out.println(str+"這是父類的方法"); } public void printfOnly(String str){ System.out.println("這是父類的方法,子類沒有重寫的方法====>"+str); } }
二、super不能在main中使用,因為main方法為靜態方法