C# 限制派生中枚举的选项 class
C# limit options of enum in a derived class
所以我试图限制我在基础 class 中声明的枚举的选项,我可以 select 在派生的 class.
namespace FLYNET.Personeel
{
public enum Graad
{
Captain,
SeniorFlightOfficer,
SecondOfficer,
JuniorFlightOfficer,
Steward,
Purser
};
public abstract class VliegendPersoneelslid : Personeelslid
{
public VliegendPersoneelslid()
{
}
public override decimal BerekenTotaleKostprijsPerDag()
{
return BasisKostprijsPerDag;
}
public List<Certificaat> Certificaten { get; set; }
}
以上是我试图从基础 class 员工那里使用的枚举。
在派生的 class Cabincrew 中,我想限制可以在构造函数中 selected 的选项。如果错误的选项是selected,需要抛出指定的异常,所以有点像这样:
namespace FLYNET.Personeel
{
public class CockpitPersoneel : VliegendPersoneelslid
{
public int VliegUren { get; set; }
public Graad Graad { get; set; }
public CockpitPersoneel()
{
if (Grade = Graad.Steward || Grade = Graad.Purser)
{
throw new Exception
}
}
public override decimal BerekenTotaleKostprijsPerDag()
{
decimal kostprijsPersoneel = 0m;
decimal percentage;
return kostprijsPersoneel;
}
}
}
我知道这可能是初学者的问题(确实是 :p),但请多多包涵。
我建议使用扩展方法来隐藏选项
public enum Grade {
None, // <- zero option is often a good idea to include
Captain,
SeniorFlightOfficer,
SecondOfficer,
JuniorFlightOfficer,
Steward,
Purser };
public static class GradeExtensions {
public static bool IsCabinCrue(this Grade grade) {
return grade == Grade.Steward || grade == Grade.Purser;
}
public static bool IsCockpitPersonnel(this Grade grade) {
return grade == Grade.Captain ||
grade == Grade.SeniorFlightOfficer ||
grade == Grade.SecondOfficer ||
grade == Grade.JuniorFlightOfficer;
}
}
然后当验证 Grade
提供的值时,您可以使用扩展方法,就好像它们是枚举方法一样:
public class CabinCrue {
...
public CabinCrue(Grade grade) {
// Validation: Cabin Crue grade only
// not Exception but ArgumentException: it's argument grade that's wrong
if (!grade.IsCabinCrue())
throw new ArgumentException("Cabin crue only", "grade");
Grade = grade;
...
}
public Grade Grade {
get;
private set;
}
...
}
我觉得你逻辑有问题。您正在尝试验证构造函数中的 Grade
或 Graad
。在构造函数调用之前不能设置 属性 Graad
。您的验证将检查变量 Graad
的默认值(在您的情况下为 Captain
)。
您需要将 Graad
传递给构造函数才能进行验证:
public CockpitPersoneel(Graad g)
{
if (g = Graad.Steward || g = Graad.Purser)
{
throw new Exception("wrong choice");
}
else
{
this.Graad = g;
}
}
您还可以将验证逻辑直接放入完整 属性 并在那里抛出异常:
Graad _myGrade;
public Graad GradeOfPerson
{
get { return _myGrade; }
set
{
if (value = Graad.Steward || value = Graad.Purser)
{
throw new Exception("Not Allowed");
}
_myGrade = value;
}
}
这样您就可以将构造函数留空:
public CockpitPersoneel()
{
}
所以我试图限制我在基础 class 中声明的枚举的选项,我可以 select 在派生的 class.
namespace FLYNET.Personeel
{
public enum Graad
{
Captain,
SeniorFlightOfficer,
SecondOfficer,
JuniorFlightOfficer,
Steward,
Purser
};
public abstract class VliegendPersoneelslid : Personeelslid
{
public VliegendPersoneelslid()
{
}
public override decimal BerekenTotaleKostprijsPerDag()
{
return BasisKostprijsPerDag;
}
public List<Certificaat> Certificaten { get; set; }
}
以上是我试图从基础 class 员工那里使用的枚举。 在派生的 class Cabincrew 中,我想限制可以在构造函数中 selected 的选项。如果错误的选项是selected,需要抛出指定的异常,所以有点像这样:
namespace FLYNET.Personeel
{
public class CockpitPersoneel : VliegendPersoneelslid
{
public int VliegUren { get; set; }
public Graad Graad { get; set; }
public CockpitPersoneel()
{
if (Grade = Graad.Steward || Grade = Graad.Purser)
{
throw new Exception
}
}
public override decimal BerekenTotaleKostprijsPerDag()
{
decimal kostprijsPersoneel = 0m;
decimal percentage;
return kostprijsPersoneel;
}
}
}
我知道这可能是初学者的问题(确实是 :p),但请多多包涵。
我建议使用扩展方法来隐藏选项
public enum Grade {
None, // <- zero option is often a good idea to include
Captain,
SeniorFlightOfficer,
SecondOfficer,
JuniorFlightOfficer,
Steward,
Purser };
public static class GradeExtensions {
public static bool IsCabinCrue(this Grade grade) {
return grade == Grade.Steward || grade == Grade.Purser;
}
public static bool IsCockpitPersonnel(this Grade grade) {
return grade == Grade.Captain ||
grade == Grade.SeniorFlightOfficer ||
grade == Grade.SecondOfficer ||
grade == Grade.JuniorFlightOfficer;
}
}
然后当验证 Grade
提供的值时,您可以使用扩展方法,就好像它们是枚举方法一样:
public class CabinCrue {
...
public CabinCrue(Grade grade) {
// Validation: Cabin Crue grade only
// not Exception but ArgumentException: it's argument grade that's wrong
if (!grade.IsCabinCrue())
throw new ArgumentException("Cabin crue only", "grade");
Grade = grade;
...
}
public Grade Grade {
get;
private set;
}
...
}
我觉得你逻辑有问题。您正在尝试验证构造函数中的 Grade
或 Graad
。在构造函数调用之前不能设置 属性 Graad
。您的验证将检查变量 Graad
的默认值(在您的情况下为 Captain
)。
您需要将 Graad
传递给构造函数才能进行验证:
public CockpitPersoneel(Graad g)
{
if (g = Graad.Steward || g = Graad.Purser)
{
throw new Exception("wrong choice");
}
else
{
this.Graad = g;
}
}
您还可以将验证逻辑直接放入完整 属性 并在那里抛出异常:
Graad _myGrade;
public Graad GradeOfPerson
{
get { return _myGrade; }
set
{
if (value = Graad.Steward || value = Graad.Purser)
{
throw new Exception("Not Allowed");
}
_myGrade = value;
}
}
这样您就可以将构造函数留空:
public CockpitPersoneel()
{
}