2014-09-01 223 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 
}