规则18:保护域中的访问方法(Protected Access Methods) 在基类中将组件参照放置在私有域中,而为这些组件添加一些访问函数来得到他们的属性,这将是一种更好的方法。
基于12个网页-相关网页
When fields are declared protected, there is the possibility that methods in subclasses will directly access them, effectively increasing the coupling within a class hierarchy.
将字段声明为受保护时,子类中的方法就有可能直接访问它们,这样就可能有效地增加类层次结构中的耦合。
The first and most important addition was access protection for methods and instance variables on classes -- the public, protected, and private keywords.
第一个也是最重要的新特性是针对类的方法和实例变量的访问保护 —— public、protected 和 private 关键字。
That's why the Vehicle class, and its subclasses, have either private or protected fields, and public setter and getter methods to provide access.
这也正是 Vehicle 类及其子类都具有 private 或者 protected 属性,而用 public setter 和 getter 方法来访问属性的原因。
应用推荐