java - 在子类中调用方法 [英] java - calling a method in a subclass
问题描述
嘿那里,我正在尝试调用子类中的方法,savingsaccount。当我调用这个方法时,它涉及一个名为balance的称为account的超类中的字段。当我尝试在该方法中涉及此字段时,它表示该字段是私有的,无法访问。有没有办法保持场地私密?我们不应该更改访问类型。
Hey there, I'm trying to call a method in a subclass, savingsaccount. when i call this method, it involves a field called balance in the superclass, called account. When i try to involve this field in the method, it says that the field is private and cannot be accessed. Is there a way around this with keeping the field private? We are not supposed to change the access type.
亲切的问候和对任何帮助的赞赏
Kind regards and much appreciation for any help
推荐答案
没有。超类应该提供适当的方法来适当地访问字段的值,可能执行验证。
No. The superclass should provide appropriate methods to access the field's value appropriately, possibly performing validation.
使字段私有的全部意义是阻止其他类访问它直接 - 相反,他们必须通过您公开的方法。
The whole point of making a field private is to stop other classes from accessing it directly - instead they have to go through the methods you expose.
这篇关于java - 在子类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!