```
```
```
///
/// 枚举作为状态机
///
public abstract class ReservationStatus : SmartEnum
{
private ReservationStatus(string name, int value) : base(name, value) { }
public abstract bool CanTransitionTo(ReservationStatus next);
public static readonly ReservationStatus New = new NewStatus();
public static readonly ReservationStatus Accepted = new AcceptedStatus();
public static readonly ReservationStatus Paid = new PaidStatus();
public static readonly ReservationStatus Cancelled = new CancelledStatus();
private sealed class NewStatus : ReservationStatus
{
public NewStatus() : base("New", 0)
{
}
public override bool CanTransitionTo(ReservationStatus next) =>
next == ReservationStatus.Accepted || next == ReservationStatus.Cancelled;
}
private sealed class AcceptedStatus : ReservationStatus
{
public AcceptedStatus() : base("Accepted", 1)
{
}
public override bool CanTransitionTo(ReservationStatus next) =>
next == ReservationStatus.Paid || next == ReservationStatus.Cancelled;
}
private sealed class PaidStatus : ReservationStatus
{
public PaidStatus() : base("Paid", 2)
{
}
public override bool CanTransitionTo(ReservationStatus next) =>
next == ReservationStatus.Cancelled;
}
private sealed class CancelledStatus : ReservationStatus
{
public CancelledStatus() : base("Cancelled", 3)
{
}
public override bool CanTransitionTo(ReservationStatus next) =>
false;
}
}
///
/// 枚举可以初始化
///
public sealed class TestEnum : SmartEnum
{
private TestEnum(string name, int value) : base(name, value)
{
}
public static readonly TestEnum One = new TestEnum(nameof(One), 1);
public static readonly TestEnum Two = new TestEnum(nameof(Two), 2);
public static readonly TestEnum Three = new TestEnum(nameof(Three), 3);
}
///
/// 别名
///
public sealed class TestEnum1 : SmartEnum
{
//真实的值通过构造函数来设定
private TestEnum1(string name, ushort value) : base(name, value) { }
///
/// One看作是别名
///
public static readonly TestEnum1 One = new TestEnum1("a", 1);
public static readonly TestEnum1 Two = new TestEnum1("b", 2);
public static readonly TestEnum1 Three = new TestEnum1("a", 3);
}
///
/// 枚举也可以被继承
/// 也可以是抽象
///
public abstract class EmployeeType : SmartEnum
{
private EmployeeType(string name, int value):base(name, value) { }
public abstract decimal BonusSize { get; }
public static readonly EmployeeType Manager = new ManagerType();
public static readonly EmployeeType Assistant = new AssistantType();
private sealed class ManagerType : EmployeeType
{
public ManagerType():base("Manager",1)
{
}
public override decimal BonusSize => 10m;
}
private sealed class AssistantType : EmployeeType
{
public AssistantType() : base("Assistant",2)
{
}
public override decimal BonusSize => 20m;
}
}
```