代码之家  ›  专栏  ›  技术社区  ›  Haraldur

派生类中枚举的C#极限选项

  •  -2
  • Haraldur  · 技术社区  · 7 年前

    所以我试图限制我在基类中声明的枚举的选项,我可以在派生类中选择。

    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; }
    }
    

    上面是我试图从基类员工那里使用的枚举。

    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),但请容忍我。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Dmitry Bychenko    7 年前

    我建议使用

    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;
      }
      ...
    } 
    
        2
  •  0
  •   Mong Zhu Bart de Boer    7 年前

    我认为你在逻辑上有错误。您正在尝试验证 Grade Graad 在构造函数中。财产 无法在构造函数调用之前设置。您的验证将检查变量的默认值 Captain ).

    你需要通过考试 进入构造函数,以便能够进行验证:

    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()
    {
    }