[英]Declaring subclasses variable in superclass
My superclass have 8 subclasses such that variables in first 4 subclasses are of a kind and remaining 4 subclasses are of another kind. 我的超类有8个子类,因此前4个子类中的变量是一种,其余4个子类是另一种。 Should it be right practice to declare all variables in my superclass with getter and setter method, and then access from respective subclasses?
在我的超类中使用getter和setter方法声明所有变量,然后从相应的子类进行访问是否应该是正确的做法? What will be advantage and disadvantage of this?
这有什么优点和缺点?
Hard to say since we don't see what the exact situation is. 很难说,因为我们没有看到确切的情况。
For the way you describe it it would probably be best to make this 3-level subclassing 对于您描述它的方式,最好进行这种三级子类化
superclass
(empty)
|
--------------+--------------
| |
subclass_typeA subclass_typeB
(4 type A properties) (4 type B properties)
Then each of these sublclass_typeX has it's own 4 subclasses 然后每个sublclass_typeX都有自己的4个子类
PROS : readability, easier later modifications 优点 :可读性强,以后修改更容易
CONS : more work for you in the beginning 缺点 :一开始会为您做更多的工作
EDIT: it would most definetly be the right practice to declare all properties in superclass. 编辑:在超类中声明所有属性绝对是正确的做法。 In that case you could use just one class with added enumerated property called
classType
but that has nothing to do with OOP anymore. 在那种情况下,您可以只使用一个带有添加的枚举属性
classType
类,但该类与OOP无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.