English 中文(简体)
私人假肢
原标题:Do Subclasses Inherit Private Instance Variables From Superclasses
最佳回答

私人成员也继承。 为了测试这一点,你可以在超级舱内做以下工作:

//...
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;
}
//...

在继承类别中创造一种方法:

//...
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 calls setConstantValue ToMyText some where.

问题回答

进入者将罚款。 认定进入者在超级阶级的“文字”中操作,因此,进入者将能够看到该成员隐藏在子类中。

至于教科书,这取决于你的观点。 该子公司继承私人成员,其意思是,他们实际上在子类别内(因此,他们拿着记忆等),但下级无法直接接触他们。

这里的“继承”是指你不能获得。 仍然存在,绝非是你可以与你进行互动(但要求超级阶级的非私人方法除外)。

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.

可在《 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. Nevertheless, a private field is never inherited by a subclass.

如果子类是同一包裹,它继承私人成员,否则它只继承公共和受保护的成员!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...