面条类及子类
#region 面条类及子类
///
/// 面条类
///
public abstract class Noodles
{
public Noodles()
{
}
///
/// 获取面类型名称
///
///
public abstract string GetName();
}
///
/// 窄面条
///
public class NarrowNoodles:Noodles
{
public NarrowNoodles()
{
}
public override string GetName()
{
return "窄面条";
}
}
///
/// 宽面条
///
public class WideNoodles : Noodles
{
public WideNoodles()
{
}
public override string GetName()
{
return "宽面条";
}
}
#endregion
卤菜类及子类
#region 卤菜类及子类
///
/// 卤菜类
///
public abstract class Halogen
{
public Halogen()
{
}
///
/// 获取卤菜名称
///
///
public abstract string GetName();
}
///
/// 西红柿鸡蛋卤
///
public class TomatoAndEgg : Halogen
{
public TomatoAndEgg()
{
}
public override string GetName()
{
return "西红柿鸡蛋卤";
}
}
///
/// 茄子豆瓣酱
///
public class AubergineBeanPaste:Halogen
{
public AubergineBeanPaste()
{
}
public override string GetName()
{
return "茄子豆瓣酱";
}
}
#endregion
打卤面类及子类
#region 打卤面类及子类
///
/// 打卤面类
///
public class NoodlesAndHalogen
{
protected string name;
protected Noodles noodles;
protected Halogen halogen;
public NoodlesAndHalogen(string name, Noodles noodles, Halogen halogen)
{
this.name=name;
this.noodles = noodles;
this.halogen = halogen;
}
///
/// 获取名称及成分
///
public void GetName()
{
Console.WriteLine("我是"+name+",由"+noodles.GetName () +"和"+halogen.GetName ()+"组成");
}
}
///
/// 西红柿鸡蛋面
///
public class TomatoEggNoodles : NoodlesAndHalogen
{
public TomatoEggNoodles(string name, Noodles noodles, Halogen halogen)
: base(name, noodles, halogen)
{
}
}
///
/// 苏格兰打卤面
///
public class ScotlandNoodles : NoodlesAndHalogen
{
public ScotlandNoodles(string name, Noodles noodles, Halogen halogen)
: base(name, noodles,halogen)
{
}
}
#endregion
客户端代码:
static void Main(string[] args)
{
NoodlesAndHalogen n1=new TomatoEggNoodles("西红柿鸡蛋面(宽面)",new WideNoodles(),new TomatoAndEgg() );
n1.GetName();
NoodlesAndHalogen n2 = new ScotlandNoodles("苏格兰打卤面(窄面)", new NarrowNoodles(), new AubergineBeanPaste());
n2.GetName();
Console.Read();
}
运行结果: