答案是看发起的类。当main函数中定义的是父类的时候,父类直接调起父类自己的Methodfather方法,此方法直接调用父类的方法。如结果1显示
当main函数中定义的是子类的时候,虽然子类中没有Methodfather方法。那就应该是直接调用父类的这个方法。但是methodfather方法中的线程调用的method方法是子类的。
原则就是调起的类是哪个,就先找哪个类中的方法。
父类
package readcode;
public class testRunFather {
void Method(String c) {
System.out.println("this is father"+ c);
}
void Methodfather() {
Runnable runnable = new Runnable(){
public void run(){
System.out.println("线程开始"+ "/");
Method("RUN");
}
};
Thread thread = new Thread(runnable);
thread.start();
// System.out.println("this is father"+ c);
}
}
子类
package readcode;
public class testRunSun extends testRunFather {
void Method(String c) {
System.out.println("this is son start"+ c);
super.Method(c);
System.out.println("this is son end"+ c);
}
void Methodfather() {
super.Methodfather();
};
}
主函数
package readcode;
public class testRunFatherSon {
public static void main(String[] args) {
testRunSun testrunfather = new testRunSun();
testrunfather.Methodfather();
//testRunFather testrunfather = new testRunFather();
//testrunfather.Methodfather();
// TODO Auto-generated method stub
}
}
主函数中调起的类是父类自己
结果1是
线程开始/
this is fatherRUN
当主函数调起的类是子类的时候
结果2是
线程开始/
this is son startRUN
this is fatherRUN
this is son endRUN