【问题标题】:How can I write constructor for this class?我怎样才能为这个类编写构造函数?
【发布时间】:2014-03-31 02:54:09
【问题描述】:
class Anything
{
    List<string> PPP = new List<string>()
    { 
        "table", 
        "chair", 
        "spoon", 
        "bread"
    };

    string name;
}

name 必须是PPP 中的一项。

name = "bed" //must be an error.

我应该如何为name 写属性? 以及Anything 类的构造函数?

例如,我可以使用 foreach 每次检查值。但我想知道还有其他方法吗? PPP很少能增值。因此我不使用枚举。 这只是我程序的一小部分。我是 OOP 的初学者。 我不使用数据库或表单。我通过 oop 在控制台中工作。

【问题讨论】:

  • “PPP 很少能增值。因此我不使用 Enum” - 我不明白这个推理。这感觉非常适合枚举...
  • 很少不是“从不”。我不太懂英语。可能是我不对。我可以很少向枚举添加项目吗?
  • 感谢您更正我的代码
  • 当然。您只需更改代码。现在所有代码都需要知道枚举 可以 被添加到,但这并不意味着它是错误的方法 - 当然,List&lt;string&gt; 也是如此。
  • 可能,但我可能会将枚举值命名为英文,并使用属性来包含描述,或者使用 i18n 的资源查找。

标签: c# .net oop properties constructor


【解决方案1】:

您可以在set 访问器中控制name

class Anything
{

    public Anything(string name)
    {
        Name = name;   // this will call the `set` accessor
    }
    List<string> PPP = new List<string>()
    { 
        "table", 
        "chair", 
        "spoon", 
        "bread"
    };

    private string name;

    public string Name
    {
        get {return name;}
        set 
        { 
             if (!PPP.Contains(value)) 
                 throw new ArgumentException("value");

             name = value;
        }
    }
}

如果性能有问题,请将List&lt;string&gt; 更改为HashSet&lt;string&gt;

【讨论】:

  • 他要求一个构造函数。他的name 是私有字段,而不是属性。
  • @SamLeach 添加了构造函数 - 我使用私有字段作为属性的支持字段。
  • @DStanley 然而,这是不可变类型和可变类型之间的区别。这是一个相当大的区别。
  • 感谢您的回答。我找到了答案,还为您的代码学到了其他东西。
【解决方案2】:

如何为这个类编写构造函数?

class Anything
{
    public Anything(string name)
    {
        if(ppp.Contains(name))
            this.name = name;
        else
            throw new ArgumentException("Name invalid");
    }

    private List<string> ppp = new List<string>()
    { 
        "table", 
        "chair", 
        "spoon", 
        "bread"
    };

    private string name;
}

您可能已经注意到我在您的代码中应用了一些 C# 约定:

  1. PPP 更改为ppp。约定是私有字段为小驼峰式。
  2. 为您的私有字段添加了显式访问修饰符。这更多是个人喜好。

【讨论】:

  • 好的,我会的。并感谢您的回答。这对我很有帮助。
【解决方案3】:
public class Anything
{
    private List<string> PPP = new List<string>()
    { 
        "table", 
        "chair", 
        "spoon", 
        "bread"
    };

    private string _name;

    // Public property to have access for reading it outside the class but 
    // with private setter to set it only inside class
    public string Name {
       get { return _name; } 
       private set { 
           // Use linq to verify name
           if(PPP.Contains(value))
           {
               _name = value;
           }
           else
           {
               string message = string.Format("{0} is not acceptable value!", value)
               throw new InvalidOperationException(message);
           }              
       } 
    };

    // Constructor
    public  Anything(string name)
    {
         this.Name = name;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 2018-03-25
    • 2019-09-04
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多