简体   繁体   English

为什么我们不能在子 class 中覆盖超级 class 的私有成员?

[英]Why can we not override private members of a super class in a sub class?

Is not the sub class inheriting all of the members from the super class regardless, so why not be able to override the private member variables from the super class?子 class 不是继承了超级 class 的所有成员,那么为什么不能覆盖超级 class 的私有成员变量呢? Would it not be safe to assume that the sub class has its own private member version and be able to override it in Java?假设子 class 有自己的私有成员版本并能够在 Java 中覆盖它,这不安全吗?

Subclasses do not inherit private members in java.子类不继承 java 中的私有成员。 Subclass doesn't know all thing about parent class子类并不了解父类 class

Would it not be safe to assume that the sub class has its own private member version and be able to override in Java?假设子 class 有自己的私有成员版本并且能够在 Java 中覆盖,这不安全吗?

NO

Just think to write a library and not just code for only your current project.想想写一个库,而不仅仅是为你当前的项目编写代码。

Your classes can be inherited to add more functionality, but the user of your library should not change your behavior.可以继承您的类以添加更多功能,但您的库的用户不应更改您的行为。

A simple example could be the AtomicInteger Java class, it has private volatile int value;一个简单的例子是AtomicInteger Java class,它有private volatile int value; field.场地。 Now, if you could access at that member just inheriting the class, would mean that you could change the value loosing all AtomicInteger thread safety.现在,如果您可以访问仅继承 class 的成员,则意味着您可以更改失去所有AtomicInteger线程安全的值。

I'm sure that if you look in java.util package you will find a lot of similar cases, in which some field is and must be managed internally and accessed only from methods我敢肯定,如果您查看java.util package 您会发现很多类似的情况,其中某些字段是并且必须内部管理并且只能从方法访问

You've gotten some hints from the other info, but here's an example.您已经从其他信息中获得了一些提示,但这里有一个示例。 Imagine two classes, Foo and Bar.想象两个类,Foo 和 Bar。 Bar inherits from Foo. Bar 继承自 Foo。 Here's a simple implementation of each:这是每个的简单实现:

public class Foo {
    private void myPrivateMethod() { System.out.printf("myPrivateMethod()\n"); }
    public void myPublicMethod() { System.out.printf("myPublicMethod()\n"); }
}

public class Bar extends Foo {
    public void barPublicMethod() {
        System.out.printf("This is barPublicMethod()!\n");

        myPublicMethod();
        myPrivateMethod();
    }

    public static void main(String[] args) {
        System.out.printf("This is main!\n");

        Bar bar = new Bar();
        bar.barPublicMethod();
    }
}

If you try to compile this code, you get this error:如果您尝试编译此代码,则会收到以下错误:

$ javac Foo.java Bar.java && java Bar
Bar.java:6: error: cannot find symbol
        myPrivateMethod();
        ^
  symbol:   method myPrivateMethod()
  location: class Bar
1 error

If you remove the call to myPrivateMethod(), it works.如果您删除对 myPrivateMethod() 的调用,它会起作用。 This is the nature of private methods.这是私有方法的本质。

Bar knows NOTHING about the private methods of Foo. Bar 对 Foo 的私有方法一无所知。 NOTHING.没有什么。 Which means you can't override them, because Bar doesn't even know they exist.这意味着您不能覆盖它们,因为 Bar 甚至不知道它们存在。 That's what private means.这就是私人的意思。

Bar can't do one single thing with private methods from Foo. Bar 不能用 Foo 的私有方法做一件事。 Nothing.没有什么。 If you want Bar to be able to override them, you need to change the definition in Foo to protected or public.如果您希望 Bar 能够覆盖它们,则需要将 Foo 中的定义更改为 protected 或 public。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM