我想创建一个具有多个状态的类对象,每次只能激活一个状态。例如:
班级飞机有三个条件:飞行,驾驶,站立
我喜欢这样的条件:
Plane boeing747 = new Plane;
boeing747.State = flying
或
boeing747.State.flying = true;
if(boeing747.State.flying == true);
console.writeline("it flyes");
else if(boeing747.State.driving == true)
console.writeline("it drives");
else
console.writeline("nothing goes");
// console reads: "it flyes"
boeing747.State.driving = true;
if(boeing747.State.flying == true);
console.writeline("it flyes");
else if(boeing747.State.driving == true)
console.writeline("it drives");
else
console.writeline("nothing goes");
// console reads: "it drives"
答案 0 :(得分:6)
您可以使用枚举:
public enum PlaneState
{
Flying,
Driving,
Standing,
}
在你班上:
public PlaneState State
{
get;
set;
}