简体   繁体   English

构建超类

[英]Structuring SuperClasses

Say i have 3 classes like this: 说我有3个这样的课程:

   |Abstract Class: Building|
   |int windows;            |
   |int rooms;              |
          |
          |
   |Abstract Class: House extends Building|
   |int familyMembers;                    |
          |
   |Class: MobileHome extends House|
   |int wheels;                    |

Now i am trying to figure out the best way to structure my program, because obviously the number of windows and rooms will depend on the subclass of House , however all buildings have windows (at least for the sake of this program they do). 现在,我正在尝试找出构建程序的最佳方法,因为显然窗户和房间的数量将取决于House的子类,但是所有建筑物都有窗户(至少出于此程序的目的)。 So that is why they are in the building class but the number of those windows will depend on the lowest subclass because if i make another class called mansion which extends House obviously a mansion will have more windows than a mobile home. 这就是为什么它们属于建筑类,但这些窗户的数量将取决于最低的子类的原因,因为如果我创建另一个称为Mansion的类来扩展House显然,Mansion的窗户将比移动房屋多。

In the end I plan on making my building class, and other abstract classes very detailed with about a hundred different attributes at the end (the fields i have listed are just examples) so i was wondering if anyone had any idea for a clean way to construct a MobileHome object maybe passing just one object down the constructor calls rather than making constructors that take hundreds of parameters. 最后,我计划将我的建筑类和其他抽象类做得非常详细,最后有大约一百种不同的属性(我列出的字段仅是示例),所以我想知道是否有人对干净的方法有任何想法构造一个MobileHome对象可能只向构造函数调用传递一个对象,而不是使构造函数接受数百个参数。

All the examples i found online were relatively simple usually only extending one class and using only a couple of fields so making the constructors was relatively easy so I'm just looking for an elegant large scale solution. 我在网上找到的所有示例都相对简单,通常只扩展一个类并仅使用几个字段,因此使构造函数相对容易,因此,我只是在寻找一种优雅的大规模解决方案。

SideNote: If anyone has any ideas on a better way to structure these classes, maybe by breaking them up more i would appreciate any opinions. 旁注:如果有人对构建此类的更好方法有任何想法,也许可以通过将它们分解开来,我将不胜感激。

The idea with you super-class is to create the most general class as possible with fields and methods that are common to all sub-classes. 超类的想法是使用所有子类都通用的字段和方法来创建最通用的类​​。 So put as many of the 'hundreds' of parameters as you can in the parent classes. 因此,请在父类中尽可能多地放入“数百”个参数。

If you have that many parameters it might be worth working out if you can group those into classes as well and then making objects of those classes as members of yours. 如果您有这么多参数,那么也可以将它们分组到类中,然后将这些类的对象作为您的成员,那么可能值得一试。 For example in the mobile home instead of having fields describing every single detail you want to group the details together. 例如,在移动房屋中,您无需将每个细节都归类到字段中,而是希望将这些细节分组在一起。 Basically if you have hundreds of fields in one class something is up with your design, there must common subgroups to all those fields. 基本上,如果一类中有数百个字段,那么您的设计就取决于您的设计,那么所有这些字段都必须有公共的子组。

You might want to check out the creational design patterns in the gof book. 您可能要检查一下gof书中的创新设计模式 In particular builder sounds like it might be a good fit. 特别是建设者听起来像它可能是一个不错的选择。 This should give you and idea of how to structure you classes. 这应该为您提供有关如何构造类的想法。 You can find some examples in java here . 你可以找到一些Java中的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM