是否可以从 Java 中的子类访问父类的私有方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13937077/
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-10-31 14:31:00  来源:igfitidea点击:

Is it possible to access a private method of parent class from the child class in Java?

java

提问by user1501127

How do I call a private method in this type of parent class? What I have done is an ugly peice of code but it works though I don't think it's a real solution.

如何在这种类型的父类中调用私有方法?我所做的是一段丑陋的代码,但它有效,尽管我认为这不是一个真正的解决方案。

Would really appreciate some advice on how this is done. I have looked to find solution but non that have given me some clarity on the matter.

真的很感激一些关于如何做到这一点的建议。我一直在寻找解决方案,但没有让我对此事有所了解。

    public abstract class Car {

         public void passItOn(int a) {

                takesVariable(a);

         }
    private void takesVariable(int a) {

         //Process the variables
    }

the child:

孩子:

     public abstract class Wheel extends Car {

     boolean a = anotherMethod();
        if(value() != a) {
          passItOn(a);
        }

     }

There is some issue with my accaptance rate but I am on it! All will give + marks for replies!!!

我的接受率有一些问题,但我已经解决了!所有回复都会给+分!!!

回答by Kevin K

Accessing a private field/method is possible using reflectionif the security manager allows it, but strongly discouraged.

如果安全管理器允许可以使用反射访问私有字段/方法,但强烈建议不要这样做。

Private methods are private for a reason. If you have access to the base class, making the method protectedis a far better solution.

私有方法是私有的是有原因的。如果您可以访问基类,那么制作方法protected是一个更好的解决方案。

回答by A--C

Private methods are not inherited, and so cannot be called. If you really want to have access to this method, change its access modifier keyword (protectedor public).

私有方法不会被继承,因此不能被调用。如果您确实想访问此方法,请更改其访问修饰符关键字(protectedpublic)。

回答by Aaron

Do this if you are fine with making it protected:

如果您可以对其进行保护,请执行以下操作:

public class BaseClass {
    protected void whatever() {}
}

Or this if you want to keep it private for some reason, and it will also give your base class more control over how/when "whatever" is used:

或者,如果您出于某种原因想将其保密,并且它还将使您的基类更好地控制如何/何时使用“任何”:

public class BaseClass {
    private void whatever() {}
    protected void doWhatever() {
        if(!forSomeReasonDoNotWantThisToBeUsed()) {        
            whatever();
        }
    }
}

回答by Salih Erikci

Make it protectedif you want to reach it from subclasses.

protected如果您想从子类访问它,请使用它。

回答by Amar

There is absolute no way this can be done. You cannot call a privatemethod of a class from any other class(be it the extending class). It can only be accessed inside that class itself. There is a reason it is said to be private.

绝对没有办法做到这一点。您不能private从任何其他类(无论是扩展类)调用类的方法。它只能在该类本身内部访问。据说是有原因的private

If you want it to be accessed by only the extending classes an not the outside world then use protectedaccess modifier for your function.

如果您希望它只被扩展类访问,而不是外部世界,那么请protected为您的函数使用访问修饰符。