class Program
{
public enum eUnitType
{
Larva,
Drone,
Hydra
}
static void Main(string[] args)
{
Unit larva1 = new Larva();
Unit drone1 = larva1.Morph(eUnitType.Drone);
drone1.Harvest();
Unit larva2 = new Larva();
Unit hydra1 = larva2.Morph(eUnitType.Hydra);
hydra1.Attack();
}
}
class Unit
{
protected string name;
//생성자
public Unit()
{
Console.WriteLine("{0}: Unit 클래스의 생성자", this);
}
//멤버 메서드
public virtual Unit Morph(Program.eUnitType unitType)
{
switch (unitType)
{
case Program.eUnitType.Drone:
Console.WriteLine("{0}가 {1}으로 변태합니다.", this, unitType);
return new Drone();
case Program.eUnitType.Hydra:
Console.WriteLine("{0}가 {1}로 변태합니다.", this, unitType);
return new Hydra();
default:
return new Larva();
}
}
public virtual void Harvest()
{
}
public virtual void Attack()
{
}
public void Move()
{
Console.WriteLine("{0}이 움직입니다.", this);
}
}
class Larva : Unit
{
//생성자
public Larva()
{
Console.WriteLine("{0}: Larva 클래스의 생성자", this);
}
//멤버 메서드
public override Unit Morph(Program.eUnitType unitType)
{
switch (unitType)
{
case Program.eUnitType.Drone:
Console.WriteLine("{0}가 {1}으로 변태합니다.", this, unitType);
return new Drone();
case Program.eUnitType.Hydra:
Console.WriteLine("{0}가 {1}로 변태합니다.", this, unitType);
return new Hydra();
default:
return new Larva();
}
}
}
class Drone : Unit
{
//생성자
public Drone()
{
Console.WriteLine("{0}: Drone 클래스의 생성자", this);
}
//멤버 메서드
public override void Harvest()
{
Console.WriteLine("{0}이 자원을 채취합니다.", this);
}
}
class Hydra : Unit
{
//생성자
public Hydra()
{
Console.WriteLine("{0}: Hydra 클래스의 생성자", this);
}
//멤버 메서드
public override void Attack()
{
Console.WriteLine("{0}가 공격을 합니다.", this);
}
}