//早餐
public abstract class Breakfast {
public abstract int Cost();
}
//主食-吐司
public class Toast: Breakfast
{
public Toast() {
}
public override int Cost()
{
return 10;
}
}
//配料-起司
public class Cheese : Breakfast
{
private Breakfast _breakfast;
public Cheese(Breakfast breakfast)
{
_breakfast = breakfast;
}
public override int Cost()
{
return 20 + _breakfast.Cost(); ;
}
}
//配料-火腿
public class Ham : Breakfast
{
private Breakfast _breakfast;
public Ham(Breakfast breakfast)
{
_breakfast = breakfast;
}
public override int Cost()
{
return 15+ _breakfast.Cost();
}
}
private void Main()
{
Breakfast b = new Toast();
b = new Ham(b);
b = new Cheese(b);
Console.WriteLine( "價錢 :"+ b.Cost().ToString());
}
沒有留言:
張貼留言