日期:2014-05-20 浏览次数:20942 次
namespace 装饰模式
{
class 员工
{
public string 员工姓名;
public double 基本工资 = 0;
public double 出勤 = 0;
public double 清凉 = 0;
public double 水电 = 0;
public 员工()
{
}
public 员工(string 员工姓名)
{
this.员工姓名 = 员工姓名;
}
public virtual string 显示()
{
return 员工姓名;
}
}
class 工资:员工
{
protected 员工 员工类型变量;
public void 装饰(员工 员工类型变量)
{
this.员工类型变量 = 员工类型变量;
}
public override string 显示()
{
if (this.员工类型变量 != null)
{
return this.员工类型变量.显示();
}
return "";
}
}
class 基础工资 : 工资
{
public override string 显示()
{
基本工资 = 3200;
return base.显示()+"\n基本工资:"+基本工资;
}
}
class 出勤奖:工资
{
public override string 显示()
{
出勤 = 500;
return base.显示() + "\n出勤:" + 出勤;
}
}
class 清凉补贴:工资
{
public override string 显示()
{
清凉 = 400;
return base.显示() + "\n清凉:" + 清凉;
}
}
class 水电扣除:工资
{
public override string 显示()
{
水电 = 300;
return base.显示() + "\n水电:" + 水电;
}
}
}
namespace 装饰模式
{
class Program
{
static void Main(string[] args)
{
员工 员工一 = new 员工("周润发");
基础工资 基础 = new 基础工资();
出勤奖 出勤 = new 出勤奖();
清凉补贴 清凉 = new 清凉补贴();
水电扣除 水电 = new 水电扣除();
基础.装饰(员工一);
出勤.装饰(基础);
清凉.装饰(出勤);
水电.装饰(清凉);
Console.WriteLine(水电.显示());
Console.Read();
}
}
}