Java 从子类访问父类的私有实例变量?

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

Accessing private instance variables of parent from child class?

javaoopclassinheritancepolymorphism

提问by lfaraone

Let's say we have a class foowhich has a private instance variable bar.

假设我们有一个foo具有私有实例变量的类bar

Now let us have another class, baz, which extends foo. Can non-static methods in bazaccess foo's variable barif there is no accessor method defined in foo?

现在让我们有另一个类,baz,其中extends foo。如果在 中没有定义baz访问器方法,可以在accessfoo的变量中使用非静态方法吗?barfoo

I'm working in Java, by the way.

顺便说一下,我在 Java 工作。

采纳答案by Wim Coenen

No, not according to the java language specification, 3rd edition:

不,不是根据java 语言规范,第 3 版

6.6.8 Example: private Fields, Methods, and Constructors

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.

6.6.8 示例:私有字段、方法和构造函数

私有类成员或构造函数只能在包含成员或构造函数声明的顶级类(第 7.6 节)的主体内访问。它不会被子类继承。

But regardless of this language restriction, you canaccess private fields through reflection:

但是不管这种语言限制如何,您都可以通过反射访问私有字段

Field privateStringField = 
   MyClass.class.getDeclaredField("privateString");
privateStringField.setAccessible(true);

String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);

回答by RichieHindle

No, for that you should use protected.

不,为此你应该使用 protected.

回答by alphazero

Child classes can not access private members (which is the whole point of private access control).

子类不能访问私有成员(这是私有访问控制的重点)。

回答by Sandro

For questions like this, where is a table found on the website here: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

对于这样的问题,可以在以下网站上找到表格:http: //java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Basically you want public or protected variable to be declared in foo since these are the variables that subclasses inherit from their parent and therefore seen in baz.

基本上,您希望在 foo 中声明 public 或 protected 变量,因为这些是子类从其父类继承的变量,因此可以在 baz 中看到。

回答by ivmos

...if there is no accessor method defined in foo?

...如果在 foo 中没有定义访问器方法?

You need accessors. Besides, take care of inheritance, Should that var really be in parent? Remember IS-A check..

你需要访问器。此外,照顾继承,那个var真的应该在父母中吗?记住 IS-A 支票..

回答by George Armhold

You cannot access private variables in descendent classes. Normally you'd want to use "protected" or "package" (the default) level access for this. However if you want to be really tricky, you can resort to using reflection and AccessibleObjectto get at it. I wouldn't recommend doing that for production code unless you are really in a bind; for testing, etc., it's fine.

您不能访问后代类中的私有变量。通常,您希望为此使用“受保护”或“包”(默认)级别的访问权限。然而,如果你想变得非常棘手,你可以诉诸于使用反射和AccessibleObject来解决它。除非您真的陷入困境,否则我不建议对生产代码这样做;用于测试等,没问题。

回答by Sushim Mukul Dutta

To use a private variable of a super class in a sub class, an accessor method is required. Else use the protected modifier instead of private.

要在子类中使用超类的私有变量,需要一个访问器方法。否则使用受保护的修饰符而不是私有的。

回答by neeranzan

The private variable(s) of a class invariably has a scope inside that class. If it has to be shared among the subclasses, it should be declared "protected"

类的私有变量总是在该类内部有一个作用域。如果必须在子类之间共享,则应将其声明为“受保护”

回答by user12333979

Private members exist(inherited) in instances of child classes .Since, object of Sub class is also an object of Super class, but it is not visible for the sub class

私有成员存在(继承)在子类的实例中。由于子类的对象也是超类的对象,但对子类不可见

They are accessible indirectly through non-private methods of Super class. These methods can access and manipulate private members

它们可以通过 Super 类的非私有方法间接访问。这些方法可以访问和操作私有成员