最近我参加了一次面试,被问到这个问题:
问题: 使用以下实体,设计一个类图或框架代码:
实体是:
服装
衬衫
裤子
Fabric
纽扣
zipper
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 站点!)
最佳答案
我觉得你想多了,我是这样看的。裤子和衬衫是服装
。服装由 Fabric 制成。
裤子有 zipper ,衬衫有纽扣..
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/