我应该将属性分成子类吗? [英] Should I separate attributes into subclasses?

查看:73
本文介绍了我应该将属性分成子类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用串行端口和文件方法构建应用程序.当我创建类时,应该将所有属性都分成子类吗?

我在Windows窗体中使用此代码,以后可能在Windows Presentation Foundation中使用.

I am building app with a serial port and file methods. When I make the classes should I separate all the attributes into subclasses?

I am using this code in Windows Forms and might later use in Windows Presentation Foundation.

class Car
{
  // is an Array of Arrays
  // I must separate the Attributes for Each of the inherited classes 
  //They are Jagged Arrays actually
  // each attribute must be worked on individually
  //this is why I ask this question
  // I also must use a CRC function in here as I save and write the files
  // along with a method for writing to a serial port
  // Am I on the right Track? or is it better to make them
  // all attributes of //the parent class only?>
  //Please Excuse I am not an Expert
  class ArrayOne Car
  {
    class Door ArrayOne
    {
      Byte handle [3]
    }
  }
  class ArrayTwo Car
  {}
  class ArrayThree Car
  {}
}



我希望在不失去修改属性的能力的情况下,使其尽可能简单或简单.我也希望保留以后将数据库附加到应用程序的可能性.预先感谢您提供任何或所有建议和帮助.这是我的第一个真实程序.



I want to make this as easy or simple as possible without losing the ability to modify attributes. I also wish to reserve the possibility of attachimg a database to the application later. Thank You in advance for any or all advice and help. This is my first real program.

推荐答案

在该示例中,其他类内部的类的定义可能不是您想要的.我认为您定义了5个类,这样的继承:
From the example, the definition of the classes inside of other classes may not be what you want. I think you defined 5 classes, inheritance like this:
Car           (inherits from Object)
+-ArrayOne    (inherits from Car)
| +-Door      (inherits from ArrayOne)(has data Byte handle[3])
|
+-ArrayTwo    (inherits from Car)
+-ArrayThree  (inherits from Car)


您可以看到只有Door拥有属性(句柄).
其他对象都没有属性.
弄清楚您希望每辆Car具有哪些属性和功能,并在其中进行定义.仅将通用代码/数据放在其中,并将特定代码放在子类中.
--------不同的解释------------
我认为这是您的意图,但不确定


You can see that only Door holds an attribute (handle).
None of the other objects have attributes.
Figure out what attributes, and functions you want every Car to have, and define it there. Only put common code/data there, and specific code in the sub classes.
--------A different interpretation------------
I think this is your intent, but not sure

;class Door
{
   public Byte[3] handle
   public void Open(){open door here}
   public void Close(){close door here}
}
class Car
{
   private int m_PassengerCapacity = 4;
   private int m_numberPassengers = 0;
   abstract public Door[] Doors   {get{}set{}}
   virtual public bool LoadPeople(int numPeople)
   {
      if(numPeople < 0)
         return UnloadPeople(numPeople);
      if(m_PassengerCapacity <= (m_numberOfPassengers+numPeople))
         return false;
      OpenDoors();
      m_numberOfPassengers += numPeople
      return true
   }
}
class DoesStuff
{
   public Car[] ArrayOne = null;
   public Car[] ArrayTwo = null;
   public Car[] ArrayThree = null;
   public void InitArrays(int l1, int l2, int l3)
   {
      ArrayOne = new Car[l1];
      ArrayTwo = new Car[l2];
      ArrayThree = new Car[l3];
      for(int idx=0;idx < l1; idx++)
         ArrayOne[idx] = new Car();
      for(int idx=0;idx < l2; idx++)
         ArrayTwo[idx] = new Car();
      for(int idx=0;idx < l3; idx++)
         ArrayThree[idx] = new Car();
   }
}



门的数量是特定于汽车类型的.
可以打开多少门以载人,这取决于汽车类型,人数和现有人数.
门的子类可以分别处理鸥翼,铰链或滑动门的打开和关闭.

这将无法编译且不完整,但我希望它能使您理解.



Number of doors is specific to car type.
How many doors to open to load people can depend on car type, num people and existing num passengers.
Door subclasses can handle opening and closing differently for gull wing, hinge or sliding doors.

This won''t compile and is not complete, but I hope it gets the idea across.


这篇关于我应该将属性分成子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆