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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:27:26  来源:igfitidea点击:

private method in inheritance in Java

javainheritanceprivate-methods

提问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 Ato 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 Bcannot invoke the private method from class A.

基于上面的代码,我对私有方法的继承感到困惑,私有方法是从class Ato继承的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 protectedis the keyword you're looking for.

如果您希望子类可以访问需要保留的超类方法private,那么protected就是您要查找的关键字。

  • Privateallows only the class containing the member to access that member.
  • Protectedallows the member to be accessed within the class and all of it's subclasses.
  • Publicallows 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 protectedand publicmethods/variables can be inherited and/or overridden.

要回答您的继承问题,B.say()是一种不同的方法 - 它甚至不是覆盖方法,A.say()因为派生类不能从其基类继承私有方法。只有protectedpublic方法/变量可以被继承和/或覆盖。

回答by Ducksauce88

Private means you can Only access it in that class an no where else.

私有意味着您只能在该类中访问它,而不能在其他地方访问它。

回答by Josh

Subclasses can only invoke or override protectedor publicmethods (or methods without access modifiers, if the superclass is in the same package) from their superclasses. privatemethods stay in the class in which they're declared and aren't visible to any other class, no matter how it's related.

子类可以仅调用或覆盖protectedpublic方法(或方法而无需访问修饰符,如果超类在同一包中)从他们的超类。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 aof type A, but the object is of type B. So when you call a.say(12);, you are looking at Bfrom the AAPI/interface/perspective.

引用a的类型为A,但对象的类型为B。因此,当您调用 时a.say(12);,您是BAAPI/接口/角度来看的。

Second, because you are looking at Bfrom the Aperspective, you are going to get an error because Ahas no public method called say(). Of course Bdoes, but remember you are treating Bas 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 Bmethods that Adoesn't know about.

其次,因为你是B从这个A角度看的,你会得到一个错误,因为A没有调用的公共方法say()。当然B可以,但请记住,您将其B视为A. 当你这样做时,你将失去任何能力(除非你稍后施法,但现在不要担心)引用那些不知道的B方法A

In the end, Bactually never inherits say()from Asince it can't see it in the first place, and Ahas 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 Aand private in Band see what happens.

现在,如果您真的想玩得开心,请将say()protected inA和private in 设置为私有,B然后看看会发生什么。