最近,我参加了一次采访,并被问到以下问题:

问题:与以下实体一起,设计类图或框架代码:

实体是:

garment shirt pant fabric buttons zip

The best I could do was this:


class Shirt : Fabric
{
  Buttons buttons {get;set;}
  //Inherits all Fabric methods

  MakeShirt()
  {
    //make a shirt here
  }
}

class Pant : Fabric
{
  Buttons buttons {get;set;}
  // Inherits all Fabric methods

  MakePant()
  {
     //Make a pant here
  }
}


class Fabric
{
  private MaterialType materialType {get;set;}
  private FabricType fabricType {get;set;}

  Fabric(MaterialType mType, FabricType fType)
  {
     this.materialType = mtype;
     this.fabricType = fType;
  }

  public GetFabricMaterial();
  public GetFabricType();

}

class Garment : IGarment
{
  Price price {get;set;}
  Audience audience {get;set;}
}

enum FabricType
{
  Fabric_Formal,
  Fabric_Casual,
}

enum MaterialType
{
   Fabric_Cotton,
   Fabric_Silk,
   Fabric_Wool,
}

class Buttons
{
Color color {get;set;}
Shape shape {get;set;}
}

class Zip
{
Color color {get;set;}
Size size {get;set;}
}


但是我仍然可以看到上面的框架代码遗漏了很多东西。


如何将Garment与其他实体关联(对象关系!?)
函数MakeShirt()MakePant()的返回类型可以是什么?
在回答这些问题时,哪种方法最好?换句话说,如何处理这类问题?


任何对此的投入表示赞赏。 (如果这不是此处要提出的正确问题,请让我知道将其移至正确的stackoverflow站点!)

最佳答案

我认为您过度考虑了这一点,我将其视为这样。裤子和衬衫是Garment。服装是用面料制成的。

裤子有拉链,衬衫有纽扣。

public enum Fabric { Cotton, Silk, Poly }

public abstract Garment{


    public Fabric Fabric {get; set; }
}

class Buttons
{
  Color color {get;set;}
  Shape shape {get;set;}
}
class Zip
{
   Color color {get;set;}
   Size size {get;set;}
}

public class Shirt : Garment{
   public Buttons Buttons { get; set;}
}
public class Pants : Garment{
  public Zip Zip { get; set;}
}

关于c# - C#中的设计类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22787349/

10-11 08:46