java 覆盖java中的“私有”方法

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

Override "private" method in java

java

提问by basel man

There something ambiguous about this idea and I need some clarifications.

这个想法有些模棱两可,我需要澄清一下。

My problem is when using this code:

我的问题是使用此代码时:

public class B {

    private void don() {
        System.out.println("hoho private");
    }
    public static void main(String[] args) {
        B t = new A();
        t.don();
    }
}

class A extends B {
    public void don() {
        System.out.println("hoho public");
    }
}

The output is hoho private.

输出是hoho private

Is this because the main function is in the same class as the method don, or because of overriding?

这是因为 main 函数与 method 在同一个类中don,还是因为覆盖?

I have read this idea in a book, and when I put the mainfunction in another class I get a compiler error.

我在一本书中读过这个想法,当我把这个main函数放在另一个类中时,我得到了一个编译器错误。

采纳答案by Elliott Frisch

You cannot override a privatemethod. It isn't visible if you cast Ato B. You canoverride a protectedmethod, but that isn't what you're doing here (and yes, here if you move your mainto Athen you would get the other method. I would recommend the @Overrideannotation when you intend to override,

您不能覆盖private方法。如果您投射AB. 你可以覆盖一个protected方法,但这不是你在这里做的(是的,如果你移动main到这里,A你会得到另一个方法。@Override当你打算覆盖时,我会推荐注释,

class A extends B {
    @Override
    public void don() { // <-- will not compile if don is private in B.
        System.out.println("hoho public");
    }
}

In this case why didn't compiler provide an error for using t.don()which is private?

在这种情况下,为什么编译器没有提供使用t.don()which is的错误private

The Java Tutorials: Predefined Annotation Typessays (in part)

Java 教程:预定义的注释类型说(部分)

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Overridefails to correctly override a method in one of its superclasses, the compiler generates an error.

虽然在覆盖方法时不需要使用此注释,但它有助于防止错误。如果标记为 的方法@Override未能正确覆盖其超类之一中的方法,则编译器会生成错误。

回答by dasblinkenlight

is this because the main function is in the same class as the method "don"

这是因为主函数与方法“don”在同一个类中吗?

No, it's because A's don()is unrelated to B's don()method, in spite of having the same name and argument list. privatemethods are hidden inside their class. They cannot be invoked directly by outside callers, such as mainmethod in your case, because they are encapsulated inside the class. They do not participate in method overrides.

不,这是因为A'sdon()B'sdon()方法无关,尽管具有相同的名称和参数列表。private方法隐藏在它们的类中。它们不能被外部调用者直接调用,例如main你的方法中的方法,因为它们被封装在类中。它们不参与方法覆盖。

回答by Nitin Dhomse

No, a private method cannot be overridden since it is not visible from any other class. You have declared a new method for your subclass that has no relation to the superclass method. One way to look at it is to ask yourself whether it would be legal to write super.func() in the Derived class.

不,私有方法不能被覆盖,因为它在任何其他类中都是不可见的。您已经为子类声明了一个与超类方法无关的新方法。看待它的一种方法是问自己在 Derived 类中编写 super.func() 是否合法。

回答by Manuel Vieda

You can't overridea private method, but you can introduce one in a derived class without a problem. The derive class can not access the private method on the ancestor.

您不能覆盖私有方法,但可以在派生类中引入一个,而不会出现问题。派生类无法访问祖先上的私有方法。

Since tis a on object of type B, calling don() method will invoque the method defined at B. It doesn't even know that there is a method named also don() at class A

由于tB类型的对象,调用 don() 方法将调用B处定义的方法。它甚至不知道在类A中还有一个名为 don() 的方法

回答by Jim W

private members aren't visible to any other classes, even children

私有成员对任何其他类都是不可见的,即使是孩子

You can't override a private method, but then again, you can't call it either. You can create an identical method with the same name in the child however.

您不能覆盖私有方法,但同样,您也不能调用它。但是,您可以在子项中创建具有相同名称的相同方法。

public class A
{
    private int calculate() {return 1;}
    public void visibleMethod()
    {
        System.out.println(calculate());
    };
}

public class B extends A
{
    private int calculate() {return 2;}
    public void visibleMethod()
    {
        System.out.println(calculate());
    };
}

If you call A.visibleMethod() it prints out 1.

如果你调用 A.visibleMethod() 它会打印出 1。

If you call B.visibleMethod() it prints 2.

如果你调用 B.visibleMethod() 它打印 2。

If you don't implement the private calculate() method in B, it won't compile because the public method that calls it can't see the private method in A.

如果你没有在B中实现私有的calculate()方法,它就不会编译,因为调用它的公共方法是看不到A中的私有方法的。