2014-09-01 244 views
1

我读了“深入浅出面向对象分析与设计”和我被困254页将Java代码转换为C#代码

在下面的Java代码上,我试图转换的“匹配”的方法到ac#之一。

public class InstrumentSpec { 

    private Map properties; 

    public InstrumentSpec(Map properties) { 
    if (properties == null) { 
     this.properties = new HashMap(); 
    } else { 
     this.properties = new HashMap(properties); 
    } 
    } 

    public Object getProperty(String propertyName) { 
    return properties.get(propertyName); 
    } 

    public Map getProperties() { 
    return properties; 
    } 

    public boolean matches(InstrumentSpec otherSpec) { 
    for (Iterator i = otherSpec.getProperties().keySet().iterator(); 
     i.hasNext();) { 
     String propertyName = (String)i.next(); 
     if (!properties.get(propertyName).equals(
      otherSpec.getProperty(propertyName))) { 
     return false; 
     } 
    } 
    return true; 
    } 
} 

这是C#代码,我到目前为止有:

public class InstrumentSpec 
{ 
    private IDictionary _properties; 

    public InstrumentSpec(IDictionary properties) 
    { 
     this._properties = properties == null ? new Hashtable() : new Hashtable(properties); 
    } 

    public object GetProperty(string propertyName) 
    { 
     return _properties.Contains(propertyName); 
    } 

    public IDictionary Properties 
    { 
     get { return _properties; } 
     set { _properties = value; } 
    } 

    public virtual bool Matches(InstrumentSpec otherSpec) 
    { 
     foreach (var prop in otherSpec.Properties) 
     { 
      if (!prop.Equals(otherSpec.Properties)) 
      { 
       return false; 

      } 
     } 
     return true; 
    } 
} 

任何人有任何想法如何使匹配方法工作,以便检查是否两个对象相匹配?

+0

心不是有一个.equals()为每个对象? – 2014-09-01 11:46:47

+0

yes在每个对象上都有一个equals()方法,正如你所看到的,我已经在我的代码中使用了它,但是我似乎无法用IDictionary来正确使用它。 – 2014-09-01 11:50:36

回答

1

看看你的比较:

if (!prop.Equals(otherSpec.Properties)) 

你什么时候预期任何单一的“财产”等于包含它的“财产”的集合?

if (!properties.get(propertyName).equals(
     otherSpec.getProperty(propertyName))) 

这基本上意味着它的循环,通过“属性”的集合,该集合是一个比较它在各个“属性”:将Java代码与“性”的对象的内部集合进行比较在另一个集合中同样命名为“财产”。但是您不在此处提及对象的集合:

private IDictionary _properties; 

您需要将一个集合中的值与另一个集合中的值进行比较。如果没有收集(我建议这样做)的实际存在值做任何检查,它可能是这个样子:

foreach (var prop in otherSpec.Properties.Keys) 
{ 
    if (!otherSpec.Properties[prop].Equals(_properties[prop])) 
    { 
     return false; 
    } 
} 
return true; 
+0

谢谢大卫,这很好。 – 2014-09-01 11:57:30

2

Java代码迭代字典键并比较各个属性值。您目前正在迭代键/值对并将它们与字典进行比较。

我猜是这样

foreach (var key in otherSpec.Properties.Keys) 
{ 
    if (!Properties[key].Equals(otherSpec.Properties[key])) 
    { 
    return false; 
    } 
} 
return true; 

会是一个更好的翻译。

+0

谢谢乔伊,它的工作原理。 – 2014-09-01 11:58:45

1

你可以完全复制的算法,如果那是你想要什么:

public virtual bool Matches(InstrumentSpec otherSpec) 
{ 
    foreach (var prop in otherSpec.Properties.Keys) 
    { 
     if (!Object.equals(properties[prop], otherSpec[prop])) 
     { 
      return false; 
     } 
    } 
    return true; 
} 

但我会建议,使用泛型,要知道,这类型,我们正在谈论

1

试试这个:

var keysEqual= Properties.Keys.SequenceEqual(otherSpec.Properties.Keys); 
    var valuesEqual = Properties.Values.SequenceEqual(otherSpec.Properties.Values); 

if(keysEqual && valueEqual) 
{ 
//objects have the same properties and values 
}