1

I am pretty new to OOP and looking into things in a bit more depth, but I have a bit of confusion between these 3 methods in C# and which one is best and what the differences are between 2 of them.

Example 1 So lets start with this one, which (so I understand) is the wrong way to do it:

public class MyClass
{
    public string myAttribute;
}

and in this way I can set the attribute directly using:

myObject.myAttribute = "something";

Example 2 The next way I have seen and that seems to be recomended is this:

public class MyClass
{
    public string myAttribute { get; set;}
}

With getters and setters, this where I dont understand the difference between the first 2 as the variable can still be set directly on the object?

Example 3 The third way, and the way that I understand the theory behind, is creating a set function

public class MyClass
{
    string myAttribute;
    public void setAttribute(string newSetting)
    {
        myAttribute = newSetting;
        //obviously you can apply some logic in here to remove unwanted characters or validate etc.
    }
}

So, what are the differences between the three? I assume example 1 is a big no-no so which is best out of 2 and 3, and why use one over the other?

Thanks

4

4 回答 4

3

第二

public class MyClass
{
  public string MyAttribute { get; set;}
}

基本上是以下的简写:

public class MyClass
{
  private string myPrivateAttribute;

  public string MyAttribute 
  { 
     get {return myPrivateAttribute;}
     set {myPrivateAttribute = value;}
  }
}

这是一个自动实现的属性,它与任何常规属性完全相同,当编译器可以为您执行此操作时,您不必实现它。

那么,什么是财产?无非就是几个方法,再加上一个名字。我可以:

public class MyClass
{
  private string myPrivateAttribute;

  public string GetMyAttribute()
  { 
     return myPrivateAttribute;
  }

  public void SetMyAttribute(string value)
  {
     myPrivateAttribute = value;
  }

}

但不是写

myClass.MyAttribute = "something";
string variable = myClass.MyAttribute;

我将不得不使用更详细但不一定更清晰的形式:

myClass.SetMyAttribute("something");
string variable = myClass.GetMyAttribute();

请注意,没有什么限制getandset方法( C# 术语中的访问器)的内容,它们是方法,就像任何其他方法一样。您可以在其中添加尽可能多或尽可能少的逻辑。即,使用自动实现的属性制作原型是有用的,然后通过显式实现添加任何必要的逻辑(例如日志属性访问或添加延迟初始化)。

于 2013-10-15T08:34:28.633 回答
0

属性是私有属性的语法糖,带有 get 和 set 方法,它非常有用且输入速度很快;

您可以将自动属性{ get; set;}视为公共属性。它没有额外的逻辑,但您可以稍后添加它而不会注意到它。只换

public string MyLine { get; set;}

string myLine;
public string MyLine 
{
    get { return myLine; }
    set { myLine = value + Environment.NewLine;  }
}

例如,如果您需要。

您还可以轻松地将只读属性创建为{ get; private set }.

所以每次都使用属性而不是公共属性只是因为它更容易和更快地编写并且它提供了更好的封装,因为如果你决定在你的程序的新版本中使用它,用户不应该使用 get 和 set 方法。

于 2013-10-15T08:58:32.190 回答
0

OOP的主要原则之一是封装,这本质上是第一个示例和其他 2 个示例之间的区别。

第一个示例您有一个直接从对象公开的私有字段- 这很糟糕,因为您允许从对象外部更改内部数据,因此无法控制它。

其他两个示例在语法上是等效的,推荐第二个示例只是因为它编写的代码更少。然而,更重要的是,它们都限制了内部数据的访问和控制突变,因此您可以完全控制数据的管理方式——这就是封装。

于 2013-10-15T08:59:58.820 回答
0

您在这里提出的问题与 OOP 语言中的封装有关。

它们之间的区别在于您可以在从类创建对象后访问对象的专有性。

在第一个示例中,new MyClass().MyAttribute无论您获取还是设置它的值,您都可以直接访问它。

在第二个示例中,您声明了 2 个用于访问它的基本函数:

public string MyAttribute 
{ 
   get {return myPrivateAttribute;}
   set {myPrivateAttribute = value;}
}

在第三个示例中,您声明了自己的设置值的方法。如果您想自定义设置器,这很有用。例如,您不想设置传递的值,而是将该值乘以 2 或其他值...

我推荐一些阅读。你可以在这里这里找到一些东西。

于 2013-10-15T08:48:04.810 回答