2014-02-16 63 views
0

我知道這是一個絕對的保姆,但不知怎的,我沒有得到它的訣竅。比方說,我有一個類Person是如下 -用類裏面的struct替換enum

public class Person 
    { 
     //Constructor... 
     public Person(string name, Gender gender) 
     { 
      this.Name = name; 
      this.Gender = gender; 
     } 

     //Member variables... 


     private string _Name; 
     public string Name 
     { 
      get 
      { 
       return _Name; 
      } 
      set 
      { 
       _Name = value; 
      } 
     } 


     private Gender _Gender; 
     public Gender Gender 
     { 
      get 
      { 
       return _Gender; 
      } 
      set 
      { 
       _Gender = value; 
      } 
     } 
    } 

    public enum Gender 
    { 
     Male = 0, 
     Female = 1 
    } 

我不想Gender是一個enum了,我想這與struct並以同樣的方式來代替,上面要將它作爲參數傳遞給Person類的constructor,這樣當我create a new object of person class I am able to assign the gender以及那個人。我怎麼做?

謝謝。

+2

而你想在struct中使用哪些字段?將它作爲枚舉更有意義。 –

+0

同意你的觀點,但如果我想要'Gender'成爲一個結構呢?這樣,用戶在創建「人」類的對象時可以通過男性或女性這樣的參數來傳遞參數嗎? –

+0

這正是枚舉所做的,不是嗎? – nima

回答

1

我還是不明白你爲什麼要做到這一點,但如果你要,你可以做這樣的事情:

class Person 
    { 
     public string Name { get; private set; } 
     public Gender Gender { get; private set; } 

     public Person(string name, Gender gender) 
     { 
      Name = name; 
      Gender = gender; 
     } 
    } 

    struct Gender 
    { 
     private int _type; 

     public bool IsMale 
     { 
      get { return _type == 0; } 
      set { _type = value ? 0 : 1; } 
     } 

     public bool IsFemale 
     { 
      get { return _type == 1; } 
      set { _type = value ? 1 : 0; } 
     } 
    } 

,並使用它:

 var p1 = new Person("John", new Gender {IsMale = true}); 
     var p2 = new Person("Jane", new Gender {IsFemale = true}); 
+0

感謝您的努力。 :) –

2

您可以以這種方式模仿enum通過struct

// Gender according ISO/IEC 5218 
// See: http://en.wikipedia.org/wiki/ISO/IEC_5218 
public struct Gender { 
    private int m_Value; 

    private Gender(int value) { 
    m_Value = value; 
    } 

    private Gender() { 
    m_Value = 0; 
    } 

    public int IsoValue { 
    get { 
     return m_Value; 
    } 
    } 

    public override int GetHashCode() { 
    return m_Value; 
    } 

    public override bool Equals(object obj) { 
    if (!(obj is Gender)) 
     return false; 

    return ((Gender) obj).m_Value == m_Value; 
    } 

    public static Boolean operator ==(Gender left, Gender right) { 
    return left.m_Value == right.m_Value; 
    } 

    public static Boolean operator !=(Gender left, Gender right) { 
    return left.m_Value != right.m_Value; 
    } 

    public static readonly Gender Male = new Gender(1); 
    public static readonly Gender Female = new Gender(2); 
    // E.g. "XYZ Corporation" 
    public static readonly Gender NotApplicable = new Gender(5); 
    // E.g. "Brown" - Mr or Mrs? 
    public static readonly Gender Unknown = new Gender(0); 
} 

... 

class Person { 
    public string Name { get; private set; } 
    public Gender Gender { get; set; } 

    public Person(string name, Gender gender) { 
    Name = name; 
    Gender = gender; 
    } 
} 
+0

非常感謝您的評論先生! :) –