码迷,mamicode.com
首页 > 其他好文 > 详细

this对象调用父类的方法

时间:2020-02-21 12:47:29      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:ring   方法   dem   style   his   结果   over   main   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()方法

this对象调用父类的方法

标签:ring   方法   dem   style   his   结果   over   main   this   

原文地址:https://www.cnblogs.com/cracker13/p/12340909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!