如何创建具有多个条件的Object

时间:2013-12-19 09:52:17

标签: c# visual-studio-2010 object properties conditional-statements

我想创建一个具有多个状态的类对象,每次只能激活一个状态。例如:

班级飞机有三个条件:飞行,驾驶,站立

我喜欢这样的条件:

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"

1 个答案:

答案 0 :(得分:6)

您可以使用枚举:

public enum PlaneState
{
    Flying,
    Driving,
    Standing,
}

在你班上:

public PlaneState State
{
    get; 
    set; 
}