gpt4 book ai didi

java - 覆盖java中不同类的方法

转载 作者:行者123 更新时间:2023-11-29 07:46:10 24 4
gpt4 key购买 nike

来自java tutorial

  • A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

  • A subclass in a different package can only override the non-final methods declared public or protected.

但有什么区别?如果一个方法没有声明为私有(private),那么它必须是公共(public)的或 protected 否?

最佳答案

这一点可以改写为:您只能覆盖您的类有权访问的方法,并且该方法不是最终的

因此,如果您在放置基类(或接口(interface))的包外部创建类,您只能覆盖非最终的publicprotected 方法。 p>

如果派生类与基类在同一个包中,那么您也可以在没有访问修饰符的情况下覆盖非 final方法(包保护)。

您永远不能覆盖私有(private)方法。

关于java - 覆盖java中不同类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25534391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com