覆盖私有方法 [英] Override private method

查看:111
本文介绍了覆盖私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读"Thinking in Java",对此有疑问.在重用类"一章的最终和私有"部分中,它说不能重写私有方法.但是,我在机器上尝试过.实际上,它可能会被覆盖.

I am reading "Thinking in Java" and have a doubt. In the chapter "reusing classes", section "final and private", it says that a private method cannot be overridden. However, I tried it on the machine. It actually could be overridden.

这是代码:

class Amphibian {
     private void print() { System.out.println("in Amphibian"); }
}

public class Frog extends Amphibian {
     public void print() { System.out.println("in Frog"); }

     public static void main(String[] args) {
          Frog f = new Frog();
          f.print();
     }
}

打印:

在青蛙中

推荐答案

您没有覆盖它,只是用同名的新方法 hid .

You didn't override it, you just hid it with a new method with the same name.

如果您没有创建新的print()方法,则您的Frog类将没有该方法.

If you didn't create a new print() method, your Frog class wouldn't have one.

这篇关于覆盖私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆