简体   繁体   English

子类是否从超类继承私有实例变量

[英]Do Subclasses Inherit Private Instance Variables From Superclasses

Do subclasses inherit private fields? 子类是否继承私有字段?

This question addresses the same problem but I don't quite understand how that satisfies the (seemingly) contradictory situations below. 这个问题解决了同样的问题,但我不太明白这是如何满足下面(看似)矛盾的情况。

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Says that "A subclass does not inherit the private members of its parent class." 说“子类不继承其父类的私有成员”。

This means that it neither inherits private instance variables nor private methods right? 这意味着它既不继承私有实例变量也不继承私有方法?

However, how does this work if it inherits a public accessor method from its parent? 但是,如果从父级继承公共访问器方法,它是如何工作的? It returns an instance variable that it doesn't know exists? 它返回一个它不知道存在的实例变量?

Also, my computer science book (Baron's AP Computer Science A) has the correct answer to a question that says that "The (Subclass) inherits all the private instance variables and public accessor methods from the (Superclass)." 此外,我的计算机科学书籍(Baron的AP计算机科学A)对一个问题的正确答案是:“(子类)从(超类)继承所有私有实例变量和公共访问器方法。”

Isn't this in contraction to oracle's tutorial? 这不是收缩到oracle的教程吗?

Thanks for your help 谢谢你的帮助

The accessor will work fine. 访问器将正常工作。 Remember that the accessor runs in the "context" of the superclass and so the accessor will be able to see the member that's hidden from the subclasses. 请记住,访问器在超类的“上下文”中运行,因此访问者将能够看到从子类隐藏的成员。

As for the textbook, it depends on your point of view. 至于教科书,这取决于你的观点。 The subclass inherits the private members in the sense that they are actually there inside instances of the subclass (so they'll take up memory, etc.), but the subclass will be unable to directly access them. 子类继承私有成员,因为它们实际上存在于子类的实例中(因此它们会占用内存等),但是子类将无法直接访问它们。

Think of it like an onion. 把它想象成一个洋葱。 Every level of hierarchy is a layer within the onion. 层次结构的每个级别都是洋葱中的一个层。 For example, If class C extends Class B, which extends class A then an object of class C would look like: 例如,如果类C扩展了类B,它扩展了类A,那么类C的对象看起来像:

Object of Class C C类对象

       -------------------------------------------------------------
       |                                                           | 
       |                   C and it's members                      |
       |                                                           |
       |    ------------------------------------------------       |
       |    |                                              |       |
       |    |              B and it's members              |       |
       |    |    ------------------------------------      |       |                                              
       |    |    |         A and it's members       |      |       |
       |    |    |                                  |      |       |
       |    |    ------------------------------------      |       |                                   
       |    ------------------------------------------------       |
       |                                                           |
       -------------------------------------------------------------            

So, an object of class C does have members of B and A. But it cannot access private members of B and A. 因此,C类的对象确实有B和A的成员。但是它不能访问B和A的私有成员。

It can however access public and protected members of B and A. 但是,它可以访问B和A的公共和受保护成员。

So a public accessor function of B or A would allow this object of class C to access a private instance variable of the class B or class A " part " of the object. 因此,B或A的公共访问器函数将允许C类的此对象访问对象的类B或类A“ 部分 ”的私有实例变量。

"Doesn't inherit" here means that you cannot access it. “不继承”这里意味着您无法访问它。 It still exists, just not in any way that you can interact with (except through calling non-private methods of the superclass). 它仍然存在,只是没有任何可以与之交互的方式(除非通过调用超类的非私有方法)。

The accepted answer in the question you linked to does explain that. 你所链接的问题中接受的答案确实解释了这一点。 Is there anything particular part of that explanation that isn't clear? 那个解释中有什么特别的部分不清楚吗?

Certainly when you create an object of a class B that inherits from a class A if the class A has private items, according to the rules of access in Java, you can not have access them , but these elements do exist in private memory, even as null references if they were null objects. 当然,当你创建一个继承自类A的类B的对象时,如果类A有私有项,根据Java中的访问规则,你不能访问它们 ,但这些元素确实存在于私有内存中,甚至如果它们是null对象,则为null引用。

In The Java Language Specification , you can read: Java语言规范中 ,您可以阅读:

A class inherits from its direct superclass and direct superinterfaces all the nonprivate fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class. 类继承自其直接超类和直接超接口超类和超接口的所有非私有字段,这些字段既可以访问类中的代码,也不会被类中的声明隐藏。

A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class. 子类可以访问超类的private字段 - 例如,如果两个类都是同一个类的成员。 Nevertheless, a private field is never inherited by a subclass. 然而, private字段永远不会被子类继承。

如果子类在同一个包中,它继承私有成员,否则它只继承public和protected成员!

Private members are inherited too. 私人成员也是继承的。 To test this, you can do the following in the superclass: 要测试它,您可以在超类中执行以下操作:

//...
private String myText = "I'm in the superclass";

private void setMyText(String myTextValue)
{
    this.myText = myTextValue;
}

public void setMyTextPublic(String myTextValue)
{
    setMyText(myTextValue);
}

public String getMyText()
{
    return myText;
}
//...

Create a method in the inherited class: 在继承的类中创建一个方法:

//...
public void setMyTextInTheSuperClass(String myTextValue)
{
    System.out.println(getMyText());
    setMyTextPublic(myTextValue);
    System.out.println(getMyText());
}

public void setConstantValueToMyText()
{
    setMyTextInTheSuperClass("I am in the child class");
}
//...

And call setConstantValueToMyText somewhere. 并在某处调用setConstantValueToMyText。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM