简体   繁体   English

在Unity中使用Getters和Setter

[英]Using Getters and Setters in Unity

I have another question about this with getters and setters. 我对getter和setter有另一个问题。 Now that I started working with c# getters and setters as I understood them. 现在我开始使用c#getters和setter,因为我理解它们。 The problem I see is that why should I make public variable that looks like this: 我看到的问题是为什么我应该创建如下所示的公共变量:

// Variable
private int _iRandomNumber
// Getter and setter
public int iRandomNumber
{
     get { return _iRandomNumber; }
     set { _iRandomNumber = value; }

}

I don't see the point of that since what different would it then be to just make the variable public since it's anyway got the get and set in the same bracket? 我没有看到这一点,因为只是将变量设为公开会有什么不同,因为无论如何得到get并设置在同一个括号中?

However if I do like this: 但是,如果我喜欢这样:

// Variable
private int _iRandomNumber
// Getter and setter
public int GetiRandomNumber { get { return _iRandomNumber; } }
public int SetiRandomNumber { set { _iRandomNumber = value; } }

Then when I try to use my SetiRandomNumber by itself Unity complier complains that I cannot use my SetProperty since I do not have a GET property inside my SET. 然后,当我尝试单独使用我的SetiRandomNumber时,Unity编译器会抱怨我无法使用我的SetProperty,因为我的SET中没有GET属性。 Should I really have to make it like the first example I wrote because as I wrote then what's the point of Getters and Setters in c#? 我是否真的必须像我写的第一个例子一样,因为在我写的时候,c#中的Getters和Setters有什么意义?

Or should I instead move away from them, like I asked from the beginning and make functions for each Get and Set like in c++ so I can actually use them by themself? 或者我应该转而离开它们,就像我从一开始就问过并为c ++中的每个Get和Set创建函数所以我实际上可以自己使用它们?

Sorry for making this a new question, however it was not possible to add this as a comment in my previous question since it was to long. 很抱歉让这个问题成为一个新问题,但是由于很长时间以来无法将此作为评论添加到我之前的问题中。

Properties allow you to fire events when values are set, for instance: 属性允许您在设置值时触发事件,例如:

public string Name {
    get { return name; }
    set {
        name = value;
        var eh = NameChanged;   // avoid race condition.
        if (eh != null)
            eh(this, EventArgs.Empty);
    }
}
private string name;
public event EventHandler NameChanged;

An added bonus is that you can track when your property gets set or read by putting breakpoints in the getter/setter with your debugger or diagnostic print statements. 另外一个好处是,您可以通过调试器或诊断打印语句在getter / setter中放置断点来跟踪何时设置或读取属性。

I don't see the point of that since what different would it then be to just make the variable public since it's anyway got the get and set in the same bracket? 我没有看到这一点,因为只是将变量设为公开会有什么不同,因为无论如何得到get并设置在同一个括号中?

The difference is that you're separating your implementation detail (a field) from your API (the property). 不同之处在于您将实现细节 (字段)与API (属性)分开。 You could later change the implementation, eg to use one long variable to serve two int fields. 您可以稍后更改实现,例如使用一个long变量来提供两个int字段。 (That's just one random example.) Or you can provide validation and change notification in the setter. (这只是一个随机的例子。)或者你可以在setter中提供验证和更改通知。 Or you can perform lazy computation in the getter. 或者您可以在getter中执行延迟计算。 Or you can make the property read-only from the outside, but writable privately. 或者您可以从外部将属性设置为只读,但可以私下写入。 The list goes on. 名单还在继续。

Your second code declares two different properties - one read-only, and one write-only, both backed by the same variable. 您的第二个代码声明了两个不同的属性 - 一个是只读的,一个是只写的,都由同一个变量支持。 That's non-idiomatic C# to say the least, and gives no benefit. 至少可以说这是非惯用的C#,没有任何好处。 There's no linkage between those two properties, whereas in the first version there's a clear link between the getter and the setter as they're parts of the same property. 这两个属性之间没有联系,而在第一个版本中,getter和setter之间存在明确的联系,因为它们是同一属性的一部分。

One thing to note is that your first example can be more concisely expressed with an automatically implemented property: 需要注意的一点是,您的第一个示例可以使用自动实现的属性更简洁地表达:

// Removed unconventional "i" prefix; this follows .NET naming conventions.
public int RandomNumber { get; set; }

That creates a private variable behind the scenes, and a public property whose getter and setter just use the private variable. 这会在幕后创建一个私有变量,以及一个getter和setter只使用私有变量的公共属性。 Later if you want to change the implementation, you can do so without affecting the API. 稍后如果要更改实现,可以在不影响API的情况下执行此操作。

The advantage of getters and setters is that they mainly act as functions so you can do something like this getter和setter的优点是它们主要充当函数,因此你可以做这样的事情

private int _iRandomNumber;

public int iRandomNumber
{
     get { return _iRandomNumber%10;} //you can do something like this mod function 
     set { _iRandomNumber = value+1000;} //you can manipulate the value being set 

} 

But if you do not have this kind of requirements on your variables, you might as well use just a public variable. 但是如果你对变量没有这种要求,你也可以只使用一个公共变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM