简体   繁体   English

如何自动实现另一个 class 的属性?

[英]How to auto-implement a property from another class?

So I need to create a class with a number of auto-implemented properties with public getters and no setters which I know how to do but source of one of the properties comes from another public class where I had to create a enum list with [Flags].因此,我需要创建一个 class ,其中包含许多带有公共 getter 的自动实现属性,而没有我知道如何做的 setter,但其中一个属性的来源来自另一个公共 class ,我必须在其中创建一个带有 [Flags 的枚举列表]。 How do I reference this property from another class?如何从另一个 class 引用此属性?

class DayoftheWeek
    {
        [Flags]
        public enum DayOfWeek
        {
            Mon = 0,
            Tue = 1,
            Wed = 2,
            Thu = 4,
            Fri = 8,
            Sat = 16,
            Sun = 32
        };
    }
class PreciseDate
    {
        public int dayMonth { get; }       
        
        //public dayWeek => DayOfWeek { get; }  ??? error
    }

If you have an enumeration that's is declared inside another class rather than being declared inside a namespace then you qualify the property type using classname.enumname.如果您有一个在另一个 class 中声明的枚举,而不是在命名空间中声明,那么您可以使用 classname.enumname 限定属性类型。 That it is a flags enum is irrelevant, as too is the fact that it is an enum.它是一个标志枚举是无关紧要的,它是一个枚举的事实也是如此。 Anything declared inside another class is qualified with the outer class name followed by the inner class name在另一个 class 中声明的任何内容都使用外部 class 名称限定,后跟内部 class 名称

Taking in the advice in the comments for better naming too, consider something like:考虑评论中的建议以获得更好的命名,请考虑以下内容:

public class EventSchedules{
  [Flags]
  public enum WeekDays { //name flags enums as plural
    Mon=1,Tue=2,Wed=4,Thu=8,Fri=16,Sat=32,Sun=64
  }
}

public class RecurringEvent{

  public EventSchedules.WeekDays OccursOn { get; private set; }
}

You should consider not to use 0 as a value for your one of your enum items unless you're going to remember to compare it numerically.您应该考虑不要使用 0 作为您的枚举项之一的值,除非您要记住以数字方式比较它。 The normal way to test for a flags enum containing a flag is like one of these:测试包含标志的标志枚举的常规方法如下:

WeekDays x = WeekDays.Tue | WeeksDays.Wed;

 //this way
if(x.HasFlag(WeekDays.Tue)) ...

//or this way
if(x & WeekDays.Tue == WeekDays.Tue) ...

If you made Mon = 0 then both of these would return true when testing against Monday ( x.HasFlag(Mon)) //returns true, isn't true ) even if the x didn't contain Monday.如果您设置了Mon = 0 ,那么即使x不包含星期一,在针对星期一( x.HasFlag(Mon)) //returns true, isn't true )进行测试时,这两个参数都将返回 true。 This is because you cannot look at a number 7 and say concretely whether it contains a 0 or not - was it 4+2+1 or 4+2+1+0?这是因为您无法查看数字 7 并具体说明它是否包含 0 - 它是 4+2+1 还是 4+2+1+0?

You could remember to do a straight compare without any masking like if(x == Mon) but then you have to wonder "did it mean that Mon was set, or that the user forgot to set a value?"您可能记得在没有任何掩码(例如if(x == Mon)的情况下进行直接比较)但随后您不得不想知道“这是否意味着设置了 Mon,或者用户忘记设置一个值?”

Maybe better to just make Mon=1 (and have a None=0)最好让 Mon=1 (并且有一个 None=0)

ps; ps; Public Things In C# Have PascalCase Names C# 中的公共事物具有 PascalCase 名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM