Java继承中的私有成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6543328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Private members in Java inheritance
提问by Mandy M
I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.
有人告诉我,对于 Java 子类,它可以继承其超类的所有成员。那么这是否意味着甚至私人成员?我知道它可以继承受保护的成员。
Can someone explain this to me. I am now totally confused.
谁可以给我解释一下这个。我现在完全糊涂了。
采纳答案by sgokhales
No, the private member are not inheritedbecause the scope of a private member is only limitedto the class in which it is defined. Only the public and protected member are inherited.
不,私有成员不会被继承,因为私有成员的范围仅限于定义它的类。只有公共成员和受保护成员被继承。
From the Java Documentation
,
Private Members in a Superclass
A subclass does not inherit the private membersof its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
超类中的私有成员
子类不继承其父类的私有成员。但是,如果超类具有访问其私有字段的公共或受保护方法,则子类也可以使用这些方法。嵌套类可以访问其封闭类的所有私有成员——包括字段和方法。因此,子类继承的公共或受保护嵌套类可以间接访问超类的所有私有成员。
From the JLS
,
从JLS
,
Members of a class that are declared private are not inheritedby 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.
声明为私有的类的成员不会被该类的子类继承。只有声明为 protected 或 public 的类的成员才能被在包中声明的子类继承,而不是在声明该类的包中。
A useful link : Does subclasses inherit private fields?
一个有用的链接:子类是否继承私有字段?
回答by Ken Wayne VanderLinde
This kind of depends on your exact usage of the word inheritance. I'll explain by example.
这取决于您对继承一词的确切用法。我会举例说明。
Suppose you have two classes: Parent
and Child
, where Child
extends Parent
. Also, Parent
has a private integer named value
.
假设您有两个类:Parent
and Child
,其中Child
extends Parent
。此外,Parent
还有一个名为 的私有整数value
。
Now comes the question: does Child
inherit the private value
? In Java, inheritance is definedin such a way that the answer would be "No". However, in general OOP lingo, there is a slight ambiguity.
现在问题来了:是否Child
继承了 private value
?在 Java 中,继承的定义方式是“否”。然而,在一般的 OOP 术语中,有一点点歧义。
You could say that it notinherited, because nowhere can Child
refer explicitly to value
. I.e. any code like this.value
can't be used within Child
, nor can obj.value
be used from some calling code (obviously).
你可以说它没有继承,因为没有任何地方可以Child
明确地引用value
. 即任何代码this.value
都不能在 内使用Child
,也不obj.value
能从某些调用代码中使用(显然)。
However, in another sense, you could say that value
isinherited. If you consider that every instance of Child
is also an instance of Parent
, then that object mustcontain value
as defined in Parent
. Even if the Child
class knows nothing about it, a private member named value
still exists within each and every instance of Child
. So in this sense, you could say that value
is inherited in Child
.
但是,从另一种意义上说,您可以说这value
是继承的。如果您认为 的每个实例Child
也是 的实例Parent
,则该对象必须包含value
中定义的内容Parent
。即使Child
类对此一无所知,名为 的私有成员value
仍然存在于 的每个实例中Child
。所以从这个意义上说,你可以说它value
是在Child
.
So without using the word "inheritance", just remember that child classes don't know about private members defined within parent classes. But also remember that those private members still exist within instances of the child class.
所以不用“继承”这个词,只要记住子类不知道父类中定义的私有成员。但也要记住,这些私有成员仍然存在于子类的实例中。
回答by gkakas
IMO by no way is it a matter of definition. In class-based Inheritance implies propagation of behavior to descendants. As such private members DO get inherited , and I will not go into the details how this happens.
IMO 绝不是一个定义问题。在基于类的继承中,意味着将行为传播给后代。由于这样的私有成员确实会被继承,我不会详细说明这是如何发生的。
Actually I find the "not inherited" answer to be dangerous for new developers and they do not comprehend right away that the private members are there hidden under the skin of your class and they (can) have severe impact on its behavior, size of the objects etc.
实际上,我发现“未继承”的答案对新开发人员来说是危险的,他们并没有立即理解私有成员隐藏在您的类的皮肤下,并且它们(可能)对其行为,大小对象等
It is common that "development comes before understanding" in computer science, however lets avoid building (or destroying) our conceptualization of OOP assuming the wrong "definition" adopted by some technician writing the manual of a well known class based OO platform.
在计算机科学中“开发先于理解”是很常见的,但是让我们避免构建(或破坏)我们的 OOP 概念,假设某些技术人员在编写基于类的知名 OO 平台的手册时采用了错误的“定义”。
Sorry for stating something in such an old post, but the issue is always valid.
很抱歉在这么旧的帖子中陈述了一些东西,但这个问题总是有效的。
回答by longe
Though https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2shows that Private Members are not inherited. Actually, it is inherited by subclass. When we use debuggers to trace variables, it will show the private members under the label of "inherited", so just try it. there is another post discussing this question, and most of them think not inherited, which misleads many people, including me at first.
虽然https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2显示私有成员不是继承的。实际上,它是由子类继承的。当我们使用调试器跟踪变量时,它会显示“继承”标签下的私有成员,所以尝试一下。还有一个帖子在讨论这个问题,大多数人认为不是遗传的,这误导了很多人,包括我一开始。
回答by Zulfequar Ali
You will be satisfied here 100%. I tested it on my computer and what I concluded I'm going to post it here. Just go through the program written below, see the program output and READ THE CONCLUSION given at the end. To test it yourself, copy the whole program and save it in a file named "InheritanceTest.java" then compile it and finally run it.
您会在这里 100% 满意。我在我的电脑上测试了它,我得出的结论是我要把它贴在这里。只需通过下面编写的程序,查看程序输出并阅读最后给出的结论。要自己测试,请复制整个程序并将其保存在名为“InheritanceTest.java”的文件中,然后编译并最终运行它。
Program
程序
// Testing if a subclass can access the private members of a superclass
class Class1 {
private String name;
public void setName(String name) {
this.name = name;
System.out.println("The name has been set successfully.");
}
public void showName() {
System.out.println("The name is: " + name);
}
}
class Class2 extends Class1 {
private int age;
public void setAge(int age) {
this.age = age;
System.out.println("The age has been set successfully.");
}
public void showAge() {
System.out.println("The age is: " + age);
}
public void displayName() {
//Accessing the private member of superclass here
//System.out.println("The name is: " + name); //error, can't compile because access to the private member name of the superclass Class1 is not permitted here.
}
}
class InheritanceTest {
public static void main(String[] args) {
Class1 c1 = new Class1();
Class2 c2 = new Class2();
c1.setName("Name_C1");
c2.setName("Name_C2"); //No error, setName() is a public member of the superclass which indirectly gives access to the private member "name".
c1.showName();
c2.showName(); //No error, showName() is a public member of the superclass which indirectly gives access to the private member "name".
c2.setAge(25);
c2.showAge();
//c2.displayName(); //error
}
}
Output
输出
The name has been set successfully.
The name has been set successfully.
The name is: Name_C1
The name is: Name_C2
The age has been set successfully.
The age is: 25
Conclusion
结论
Yes, a subclass can indirectly access the private members of a superclass. A subclass can't directly access the private members of a superclass.
是的,子类可以间接访问超类的私有成员。子类不能直接访问超类的私有成员。
All the public, private and protected members (i.e. all the fields and methods) of a superclass are inherited by a subclass but the subclass can directly access only the public and protected members of the superclass. If an inherited member from a superclass gives access to a private member of the superclass then the subclass can use this inherited member to access the private member of the superclass.
超类的所有public、private 和protected 成员(即所有字段和方法)都由子类继承,但子类只能直接访问超类的public 和protected 成员。如果从超类继承的成员可以访问超类的私有成员,则子类可以使用此继承成员访问超类的私有成员。