///
<summary>
///
通过复制,快速创建多个同类对象
///
</summary>
public
class
Program
{
public
static
void
Main()
{
Device
track1 = new
Track();
track1.ID = "101";
track1.width = 10;
track1.height = 5;
track1.weight = 30;
track1.Start();
Device
track2 = track1.Clone();
track2.ID = "102";
;
track2.Stop();
Device
light1 = new
Light();
light1.ID = "201";
light1.width = 30;
light1.height = 15;
light1.weight = 40;
light1.Start();
Device
light2 = light1.Clone();
light2.ID = "202";
light2.Stop();
}
}
abstract
public
class
Device
{
private
string
id;
public
string
ID
{
set
{ id = value;
}
get
{ return
id; }
}
public
int
width;
public
int
height;
public
int
weight;
private
string
state;
public
string
State
{
set
{ state = value;
}
get
{ return
state; }
}
public
abstract
Device
Clone();
public
abstract
void
Start();
public
abstract
void
Stop();
}
class
Track
: Device
{
public
override
Device
Clone()
{
return
(Device)this.MemberwiseClone();
}
public
override
void
Start()
{
State = "free";
Console.WriteLine("Track:
{0} ,width={1} , height={2}, weight={3},state
={4}",
ID, width, height, weight, State);
}
public
override
void
Stop()
{
State = "occupy";
Console.WriteLine("Track:
{0} ,width={1} , height={2}, weight={3},state
={4}",
ID, width, height, weight, State);
}
}
class
Light
: Device
{
public
override
Device
Clone()
{
return
(Device)this.MemberwiseClone();
}
public
override
void
Start()
{
State = "on";
Console.WriteLine("Light:
{0} ,width={1} , height={2}, weight={3},state
={4}",
ID, width, height, weight, State);
}
public
override
void
Stop()
{
State = "off";
Console.WriteLine("Light:
{0} ,width={1} , height={2}, weight={3},state
={4}",
ID, width, height, weight, State);
}
} |