this对象调用父类的方法


一.当子类没有定义方法时,this对象会寻找父类中的方法

二.

package com.cracker;


class Parent{
    
    public void action() {
        
    }
    
    public void sleep() {
        System.out.println("父类:嗷呜");
    }
}

class Child extends Parent{
    
    @Override
    public void action() {
        this.sleep();
    }
    
}

public class Demo1 {
    
    public static void main(String[] args) {
        Parent child = new Child();
        child.action();
    }
}

结果:

父类:嗷呜

 

如果子类中重写了sleep()方法,this.sleep()则调用重写的sleep()方法


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM