简体   繁体   English

在超类中声明子类变量

[英]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.

相关问题 在超类数组中初始化UIViewController子类? - Initialize UIViewController subclasses in array of superclass? iPhone-在子类上具有超类调用方法…(?) - iPhone - having a superclass calling methods on subclasses…(?) 如何访问子类中的超类变量 - How to access superclass variable in subclass 在超类中创建一个公共出口变量 - Create a common outlet variable in superclass 重复使用通过IBOutlet连接到超类的uitableview的xib:如何更好地添加仅包含某些子类的带有按钮的工具栏? - Reusing a xib with a uitableview connected via IBOutlet to a superclass: how is it better add a toolbar with a button only for some subclasses? Objective C - 子类中的窄实例变量类型? - Objective C — narrow instance variable types in subclasses? 我可以为类类型变量指定特定的超类要求吗? - Can I specify a specific superclass requirement for a class type variable? 在Objective C中声明变量时,何时不应使用星号(*) - When should you NOT use the asterisk (*) when declaring a variable in Objective C objective-c interface - 声明变量vs只是属性? - objective-c interface - declaring variable vs just property? 声明两个具有不同名称的属性,它们将指向同一变量 - declaring two properties with different name that will point to the same variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM