一.当子类没有定义方法时,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()方法

相关文章:

  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-12-30
  • 2021-07-24
  • 2021-11-09
猜你喜欢
  • 2022-12-23
  • 2018-04-18
  • 2022-12-23
  • 2021-05-25
  • 2018-11-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案