gpt4 book ai didi

java - 子类是否具有父类的所有属性和方法?

转载 作者:行者123 更新时间:2023-11-30 02:41:57 26 4
gpt4 key购买 nike

子类是否拥有父类的所有变量和方法?包括父类的私有(private)属性和方法?

或者说子类只能访问父类的变量和方法?比如公开、默认、保护?

最佳答案

来自Java Language Specification, 8.2 :

The members of a class type are all of the following:

  • Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass

  • Members inherited from any direct superinterfaces (§8.1.5)

  • Members declared in the body of the class (§8.1.6)

关于继承的重要部分,关于privatepublicprotected成员:

Members of a class that are declared private are not inherited by subclasses of that class.**

  • Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

  • Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

关于java - 子类是否具有父类的所有属性和方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41418197/

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