【问题标题】:Comparison between three members of an object对象的三个成员之间的比较
【发布时间】:2020-05-21 00:16:49
【问题描述】:

考虑以下类型的对象:

public class MyObject
{
    // "defining" attributes
    private string member1;
    private string member2;
    private string member3;

    // other attributes
    private string member4;
    private string member5;

    // ctor
    public MyObject(){}

    public bool compare(MyObject that)
    {
       // compare this object with another (that)
    }

compare() 方法的行为应如下所示。 它只考虑“定义”属性。如果它们在两个对象之间全部不同,它应该返回 false。如果它们全部相同,则返回 false。在其他情况下,返回 true(如果两个对象之间只有一个或两个不同)。

问题是,我必须为此诉诸一个巨大的if 声明吗?有没有“更好”的解决方案?

【问题讨论】:

  • 你能不能把definingString创建成List<string>而不是n个字符串变量。
  • @PrasadTelkikar 我不明白为什么不这样做。不过,我仍然不明白它如何使比较更容易。你能详细说明一下吗?
  • @PrasadTelkikar 我收到了多个关于这个问题的答案,但我还没有尝试过。我明天会这样做。
  • 如果您有任何问题,请告诉我。
  • 谢谢。感谢您的回答,看起来它会解决我的问题,但我会更新您。

标签: c# .net class oop object


【解决方案1】:

您可以创建名为List<string> DefiningAttributesList<string> OtherAttributes 的属性,而不是创建n 字符串数。

现在将值添加到您想要的列表中,现在我在构造函数中执行此操作。使用Except()方法获取DefiningAttributesOtherAttributes的区别

检查下面的实现

public class MyObject
{
    // "defining" attributes
    public List<string> DefiningAttributes { get; set; }

    // other attributes
    public List<string> OtherAttributes { get; set; }

    public MyObject()
    {
        //I used constructor to assign values
        DefiningAttributes = new List<string>() { "ABC",  "PQR",  "XYZ" };
        OtherAttributes = new List<string>() { "ABC",  "PQR",  "Stackoverflow" };
    }

    public bool compare(MyObject that)
    {
         var difference = this.DefiningAttributes.Except(that.DefiningAttributes);
         //Return false If they are all different between two objects OR if they are all same
         if(difference.Count() == this.DefiningAttributes.Count() || !difference.Any())
                return false;

          //Otherwise return true
          return true;
    }
}

更多详情,请阅读Enumerable.Except method

【讨论】:

    【解决方案2】:

    我认为应该这样做

    var comp1 = this.member1 == that.member1;
    var comp2 = this.member2 == that.member2;
    var comp3 = this.member3 == that.member3;
    
    var comparisons = new List<string>() { comp2, comp3 };
    
    return comparisons.Any(val => val != comp1 );
    

    comp1comp2comp3 将是布尔值。如果这些比较中的任何一个与第一次比较*不同,我们就知道我们有不同的结果。

    [*] 你可以使用任何参考点来代替第一个比较

    编辑: 哎呀,我以为这是一个 javascript 问题,但后来我意识到它是 C#。我只是将答案更改为使用 C# 语法,但想法是一样的。这需要 Linq 扩展方法Any

    【讨论】:

      【解决方案3】:

      下面的代码应该可以解决问题。 如果您想增加定义属性的数量,您只需编辑数组的大小或将其交换为列表即可。

      它应该遍历它们,当一个不马赫返回 true。 如果最后没有匹配项返回 false。

      public class MyObject
      {
      
          // "defining" attributes
          public string[] definingAttributes = new string[3];
      
          // other attributes
          private string member4;
          private string member5;
      
          // ctor
          public MyObject() { }
      
          public bool compare(MyObject that)
          {
              bool? previousResult = null;
              // compare this object with another (that)
              for (int i = 0; i < definingAttributes.Length; i++)
              {
                  if (previousResult == null)
                  {
                      previousResult = definingAttributes[i] == that.definingAttributes[i];
                  }
                  if (definingAttributes[i] != that.definingAttributes[i])
                  {
                      if (previousResult != (definingAttributes[i] == that.definingAttributes[i]))
                      {
                          return true;
                      }
                  }
              }
              return false;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 2014-05-03
        • 2021-07-28
        • 1970-01-01
        相关资源
        最近更新 更多