【问题标题】:C#: Inherit from Boolean?C#:从布尔继承?
【发布时间】:2011-08-24 16:40:44
【问题描述】:

(如何)我可以从布尔值继承? (或者使用 '=' 运算符使我的类与布尔类相当)

class MyClass : Boolean
{
    public MyClass()
    {
        this = true;
    }
}
class Program
{
    public Program()
    {
        MyClass myClass = new MyClass();
        if(myClass == true)
            //do something...
        else
            //do something else...
    }
}

【问题讨论】:

  • 为什么要从布尔值继承?通过继承,它必须满足“是”的关系。你的类是布尔值吗?

标签: c# inheritance operator-overloading boolean


【解决方案1】:

你不能。 System.Boolean 是结构体,不能从结构体派生。

现在,您究竟为什么要这样做?更大的目的是什么?

可以包含一个从您的类到bool 的隐式转换运算符,但我个人不会。我几乎总是更喜欢公开一个属性,所以你会写:

if (myValue.MyProperty)

...我认为这可以让事情变得清晰。但是,如果您能给我们更多真实的上下文,我们或许可以提供更具体的建议。

【讨论】:

    【解决方案2】:

    简单示例:

    public class MyClass {
        private bool isTrue = true;
    
        public static bool operator ==(MyClass a, bool b)
        {
            if (a == null)
            {
                return false;
            }
    
            return a.isTrue == b;
        }
    
        public static bool operator !=(MyClass a, bool b)
        {
            return !(a == b);
        }
    }
    

    您可以在代码中的某处将您的对象与布尔值进行比较:

    MyClass a = new MyClass();
    if ( a == true ) { // it compares with a.isTrue property as defined in == operator overloading method
       // ...
    }
    

    【讨论】:

    • 但是,我不会向任何人推荐这种模式。 == 运算符重载不清楚,因为大多数程序员将其视为对象引用相等检查或原始数据类型相等检查。在代码 (a == true) 中,“a”-variable 将被每个人视为 bool 数据类型并且会被混淆。应该避免运算符重载,不知道为什么微软在 C# 中添加了这个功能。
    【解决方案3】:

    您可以使用隐式转换运算符来获得此代码:

    class MyClass {
      public bool Value { get; set; }
      public MyClass() {
        Value = true;
      }
      public static implicit operator bool(MyClass m) {
        return m != null && m.Value;
      }
    }
    
    class Program {
      public static void Main() {
        var myClass = new MyClass();
        if (myClass) { // MyClass can be treated like a Boolean
          Console.WriteLine("myClass is true");
        }
        else {
          Console.WriteLine("myClass is false");
        }
      }
    }
    

    可以像上面那样使用:

    if (myClass) ...
    

    或者像这样:

    if (myClass == true) ...
    

    【讨论】:

      【解决方案4】:

      虽然您的示例不起作用,但您可以为自己的类做类似的事情来测试一个是否等于另一个的值。

      http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

      public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
      {
          // If both are null, or both are same instance, return true.
          if (System.Object.ReferenceEquals(a, b))
          {
              return true;
          }
      
          // If one is null, but not both, return false.
          if (((object)a == null) || ((object)b == null))
          {
              return false;
          }
      
          // Return true if the fields match:
          return a.x == b.x && a.y == b.y && a.z == b.z;
      }
      
      public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
      {
          return !(a == b);
      }
      

      【讨论】:

        【解决方案5】:

        您可以(“或使我的课程具有可比性...”),通过覆盖 == 运算符。我认为 Jon Skeet 忽略了这部分问题。

        【讨论】:

          【解决方案6】:

          如果您希望能够在“if”语句中使用您的值,请定义operator true and operator false(如果您想使用&&||,请定义&| 运算符。)( VB equivalents)

          要回答更多,我必须知道您要做什么(换句话说,为什么不直接使用bool?)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-02-23
            • 1970-01-01
            • 1970-01-01
            • 2012-07-09
            • 2015-04-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多