Java 从子类更改超类实例变量的值

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

Changing the value of superclass instance variables from a subclass

javainheritancesubclasssuperclass

提问by IBOED2

I've found that I can do it this way in the child class:

我发现我可以在子类中这样做:

ParentClass.variable = value;

But I've been told that it's better practice to use get/set methods and not give direct access to variables outside a class. Though this was for when I had an instance of the class in another class, not for subclasses and superclasses.

但是有人告诉我,最好使用 get/set 方法而不是直接访问类外的变量。虽然这是因为我在另一个类中有一个类的实例,而不是子类和超类。

So is there a better way of doing this, and which way is generally considered best practice?

那么有没有更好的方法来做到这一点,哪种方法通常被认为是最佳实践?

采纳答案by Daniel Kaplan

You have a lot of options.

你有很多选择。

  1. super.field = xYou have to have access to the field to do this
  2. field = xYou have to have access to the field to do this. You also can't have another fieldin the child or only the child's will be set.
  3. setParentField(x)I'd say this is the second best way to do it.
  4. x = callChildMethod()this code can be in the parent. The child has the implementation that returns the result. If this is possible, it's the best way to do it. See the template method pattern
  1. super.field = x您必须有权访问该字段才能执行此操作
  2. field = x您必须有权访问该字段才能执行此操作。您也不能field在孩子中拥有另一个孩子,否则只会设置孩子的遗嘱。
  3. setParentField(x)我会说这是第二好的方法。
  4. x = callChildMethod()此代码可以在父级中。孩子有返回结果的实现。如果这是可能的,这是最好的方法。查看模板方法模式

回答by Simeon Visser

For instance variables you can do the following in a method of a subclass:

对于实例变量,您可以在子类的方法中执行以下操作:

this.variable = value;

which is perfectly fine. To modify instances of other classes it's best to use getters and setters.

这很好。要修改其他类的实例,最好使用 getter 和 setter。

It's indeed true that you should prevent other classes from modifying instance variables directly; in that case it's best to use getters and setters. But in a subclass you can modify instance variables directly.

确实应该防止其他类直接修改实例变量;在这种情况下,最好使用 getter 和 setter。但是在子类中,您可以直接修改实例变量。

回答by Abed Yaseen

one of OOP principles is encapsulationsto have best practice code , and having a private members/variables , then access them using setters/getters is the way to achieve encapsulation.

OOP 原则之一是encapsulations拥有最佳实践代码,并拥有私有成员/变量,然后使用 setter/getter 访问它们是实现封装的方式 。

回答by Isuru Gunawardana

You best practice is to use getters and setters. More INFO.

您最好的做法是使用 getter 和 setter。更多信息

you can use,

您可以使用,

ParentClass.variable = value;

The best soltion depends on the requirement.

最佳解决方案取决于需求。

回答by user2885596

If there are any private member in the superclass then there is a use of setter and getter method as because we cannot use private member in its subclass.

如果超类中有任何私有成员,则使用 setter 和 getter 方法,因为我们不能在其子类中使用私有成员。

And in case of any static instance member you can directly use with the help of class name. If it is a instance member of the super class then try to access / modify that member in the subclass with the use of superkeyword. You can modify with the help of thiskeyword also what if you are having instance member in superclass and subclass with the same name? Then in this case, with the use of thiskeyword JVM will access the current class instance member i.e subclass member.

如果有任何静态实例成员,您可以在类名的帮助下直接使用。如果它是超类的实例成员,则尝试使用super关键字访问/修改子类中的该成员。您可以在关键字的帮助下进行修改,如果您在超类和子类中有同名的实例成员怎么办?那么在这种情况下,使用this关键字 JVM 将访问当前类的实例成员,即子类成员。