这种情况的最佳做法是什么? [英] What would be best practice for this scenario?

查看:60
本文介绍了这种情况的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这些属性的最佳做法是什么。我希望将杂草项目(菊苣等...)封装在Weed类中,并通过我的Windows窗体(Weeds_Data_Window)进行访问。





What would be the best practice for using these properties. I want the Weed Items (Chicory etc..) to be encapsulated within the Weed class and be accessed through my Windows Form (Weeds_Data_Window).


   public partial class Weeds

{       private string name;
        private string edible;
        private string regularity;
        
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public string Edible
        {
            get { return this.edible ; }
            set { this.edible = value; }
        }
        public string Regularity
        {
            get { return this.regularity; }
            set { this.regularity = value; }
        }
}





示例1:





Example 1:

public partial class Weeds
{
public void Chicory()
{

    Weeds Chicory = new Weeds();
    Chicory.name = "Chicory";
    Chicory.edible = "Yes";
    Chicory.regularity = "Moderate";



使用:


Use:

public partial class Weeds_Data_Window : Window
{
        Weeds cWeeds = new Weeds();
        private void CollectWeeds()
        {
            if (list_collection.SelectedItem != null)
            {
                if (list_collection.SelectedItem.ToString() == "Chicory")
                {
                    cWeeds.Chicory();
}
}





示例2:





Example 2:

public partial class Weeds
{ public void Chicory(string name, string edible, string feed)
{
    Weeds Chicory= new Weeds();
    Chicory.Name = name;
    Chicory.Edible = edible;
    Chicory.Regularity = feed;
}
}





使用:





Use:

public partial class Weeds_Data_Window : Window
{

Weeds cWeeds  = new Weed();
private void CollectWeeds()
{
    if (list_collection.SelectedItem != null)
    {
        if (list_collection.SelectedItem.ToString() == "Chicory")
        {
            cWeeds.Chicory("Chicory","Yes","Moderate");
}







我想问一些建议,其中哪些是更好地使用(如果有的话,如果还有其他更好的解决方案请建议)。



非常感谢任何帮助!谢谢。




I would like to ask for some advice for which of these would be better to use (if any, if there is another better solution please suggest).

Any help is greatly appreciated! Thank you.

推荐答案

您的代码问题不在于您所展示的差异,而在于其他部分。



首先,您的字段声明是多余的。您应该更好地使用自动实现的属性(据我记得,使用C#v.3或更高版本):



The problem of your code is not in the differences you've demonstrated, but in other parts.

First of all, your field declarations are redundant. You should better use auto-implemented properties (as far as I remember, with C# v.3 or later):

public partial class Weeds {
    public string Name { get; set; }
    // ...
}





此外,如果此类或这些属性仅在同一个程序集中使用,请考虑使用 internal 而不是 public 。你的构造函数也是如此。此外,如果您在构造函数中初始化支持字段(第二个选项,这是完全正常的),请考虑将setter保持为私有,因为构造函数可能是您允许修改字段的唯一位置:





Also, if this class or these properties are only used inside the same assembly, consider using internal instead of public. Same goes about your constructors. Besides, if you initialize the backing fields in a constructor (your second option, which is perfectly fine), consider keeping setter private, as the constructor might be the only place where you would allow to modify the fields:

public partial class Weeds { // or internal
    
    public void Chicory(string name, string edible, string feed) { // or internal
        this.Name = name;
        //...
    }

    // only reading is (public or internal):    
    public string Name { get; private set; }  // or internal instead of public

   // ...
}





因此,在构造函数中初始化字段和让属性读/写之间的选择是通过代码的语义,从便利的角度设计和其他设计考虑来定义的。没有一个明确的决定;无论哪种方式都可以。



最后,< bbiggest>你的代码的问题是使用硬编码的立即常量,如菊苣。除了在开发和第一次测试期间存在的一些初步版本之外,我无法看到可能被使用的情况。您应该使用其他东西,具体取决于您的目标。特别是,它可能是枚举,可能是资源(请参阅我的文章人类可读的枚举元数据 [ ^ ],一个明显的例子),但它可能是别的东西。你所展示的硬编码根本不可接受。



-SA



So, the choice between having the fields initialized in constructor and letting the properties read/write is defined by semantic of your code, design of it from the convenience point of view and other design consideration. There is not a one clear-cut decision; either way could be fine.

And, finally, the <bbiggest> problem of your code is the use of hard-coded immediate constants like "Chicory". I cannot see situations when it can be potentially used, except some preliminary version existing only during development and first tests. You should use something else, depending on your goals. In particular, it could be enumerations, possibly with resources (please see my article Human-readable Enumeration Meta-data[^], for a clear example), but it could be something else. Hard-coding as you demonstrated it is simply not acceptable.

—SA


#1 :确保属性永远不会被称为名称,因为它可能与标准类属性冲突。 (你可以称之为WeedName,例如。)

#2:我肯定不会使用菊苣方法,因为它没有描述它的作用,你会得到一种方法,每种新的杂草。

#3你可以在Weed类中使用不同的方法,例如默认构造函数和覆盖构造函数。

#4可食用是/否?使用布尔值。

#5根据你想要如何使用它,你也可以创建一个接口或抽象类Weeds和一个继承类Chicory。



也许你可以



保留默认构造函数

#1: Make sure a property is never called "Name" because it can clash with a standard class property. (you could call it WeedName eg.)
#2: I would most certainly NOT use a method "Chicory" because it does not describe what it does and you would get a method for each new weed.
#3 you can use different methods inside the Weed class eg a default constructor and a override constructor.
#4 Edible is Yes/No ? use a boolean.
#5 Depending on how you want to use it, you can also make an interface or abstract class Weeds and an inherited class Chicory.

Perhaps you can

keep the default constructor
public Weeds(){ 
    // init variables to defaults if necessary
}





覆盖默认构造函数



override the default constructor

public Weeds(string name, string edible, string regularity){ 
    // init variables to argument values 
}





使用属性



use the properties

public string WeedName
{
    get { return this.name; }
    set { this.name = value; }
}
public string Edible
{
    get { return this.edible ; }
    set { this.edible = value; }
}
public string Regularity
{
    get { return this.regularity; }
    set { this.regularity = value; }
}





所有在一个班级。然后使用:





all in one class. usage would then be:
or

Weeds chicory = new Weeds();
chicory.WeedName = "chicory";
chicory.Edible = "Yes";
chicory.Regularity = "Moderate;









or

Weeds chicory = new Weeds("chicory", "Yes", "Moderate");
//if necessary one could override the properties.







希望这会有所帮助。




hope this helps.


这篇关于这种情况的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆