java 父类子类方法在子进程的调用顺序

答案是看发起的类。当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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值