Java继承中的私有方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19461867/
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 method in inheritance in Java
提问by pei wang
I have confusion about using private methods in inheritance, for example:
我对在继承中使用私有方法感到困惑,例如:
public class A {
private void say(int number){
System.out.print("A:"+number);
}
}
public class B extends A{
public void say(int number){
System.out.print("Over:"+number);
}
}
public class Tester {
public static void main(String[] args) {
A a=new B();
a.say(12);
}
}
Based on the codes above, I am confused about the inheritance of private method, is the private method inherited from class A
to B
? Or the say methods in both classes are totally unrelated? As the code has error when it is running in main() method, it seems like class B
cannot invoke the private method from class A
.
基于上面的代码,我对私有方法的继承感到困惑,私有方法是从class A
to继承的B
吗?或者两个类中的 say 方法完全无关?由于代码在 main() 方法中运行时出错,似乎class B
无法从class A
.
采纳答案by nhgrif
If you want a subclass to have access to a superclass method that needs to remain private
, then protected
is the keyword you're looking for.
如果您希望子类可以访问需要保留的超类方法private
,那么protected
就是您要查找的关键字。
Private
allows only the class containing the member to access that member.Protected
allows the member to be accessed within the class and all of it's subclasses.Public
allows anyone to access the member.
Private
只允许包含该成员的类访问该成员。Protected
允许在类及其所有子类中访问成员。Public
允许任何人访问该成员。
回答by Lews Therin
The reason you are getting the error is because say(int)
is private. This has nothing to do with inheritance. You can only call a private member method in its definition class.
您收到错误的原因是因为say(int)
是私有的。这与继承无关。您只能在其定义类中调用私有成员方法。
To answer your inheritance question, B.say()
is a differentmethod - it isn't even overriding method A.say()
because derived classes can't inherit private methods from its base class. Only protected
and public
methods/variables can be inherited and/or overridden.
要回答您的继承问题,B.say()
是一种不同的方法 - 它甚至不是覆盖方法,A.say()
因为派生类不能从其基类继承私有方法。只有protected
和public
方法/变量可以被继承和/或覆盖。
回答by Ducksauce88
Private means you can Only access it in that class an no where else.
私有意味着您只能在该类中访问它,而不能在其他地方访问它。
回答by Josh
Subclasses can only invoke or override protected
or public
methods (or methods without access modifiers, if the superclass is in the same package) from their superclasses. private
methods stay in the class in which they're declared and aren't visible to any other class, no matter how it's related.
子类可以仅调用或覆盖protected
或public
方法(或方法而无需访问修饰符,如果超类在同一包中)从他们的超类。private
方法保留在声明它们的类中,并且对任何其他类都不可见,无论它如何相关。
回答by Vidya
There are two things going on here.
这里有两件事正在发生。
First, keep in mind the distinction between the type of the reference and the type of the object.
首先,请记住引用类型和对象类型之间的区别。
When you say
当你说
A a = new B();
A a = new B();
the reference is a
of type A
, but the object is of type B
. So when you call a.say(12);
, you are looking at B
from the A
API/interface/perspective.
引用a
的类型为A
,但对象的类型为B
。因此,当您调用 时a.say(12);
,您是B
从A
API/接口/角度来看的。
Second, because you are looking at B
from the A
perspective, you are going to get an error because A
has no public method called say()
. Of course B
does, but remember you are treating B
as an A
. When you do that, you lose any ability (unless you cast later, but don't worry about that for now) to reference those B
methods that A
doesn't know about.
其次,因为你是B
从这个A
角度看的,你会得到一个错误,因为A
没有调用的公共方法say()
。当然B
可以,但请记住,您将其B
视为A
. 当你这样做时,你将失去任何能力(除非你稍后施法,但现在不要担心)引用那些不知道的B
方法A
。
In the end, B
actually never inherits say()
from A
since it can't see it in the first place, and A
has no public method say()
for anyone to access.
最后,B
实际上永远不会继承say()
自,A
因为它首先看不到它,并且A
没有say()
任何人可以访问的公共方法。
Now if you want to really have some fun, make say()
protected in A
and private in B
and see what happens.
现在,如果您真的想玩得开心,请将say()
protected inA
和private in 设置为私有,B
然后看看会发生什么。