Documentation

The Java™ Tutorials
Hide TOC
Hiding Fields隐藏字段
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Inheritance

Hiding Fields隐藏场

Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. 在类中,与超类中的字段同名的字段会隐藏超类的字段,即使它们的类型不同。Within the subclass, the field in the superclass cannot be referenced by its simple name. 在子类中,超类中的字段不能由其简单名称引用。Instead, the field must be accessed through super, which is covered in the next section. 相反,必须通过super访问该字段,这将在下一节中介绍。Generally speaking, we don't recommend hiding fields as it makes code difficult to read.一般来说,我们不建议隐藏字段,因为它会使代码难以阅读。


Previous page: Polymorphism
Next page: Using the Keyword super