getter/setter VS 公共变量的好处?
使用是否有好处:
private var _someProp:String;
public function set someProp(value:String):void
{
_someProp = value;
}
public function get someProp():String
{
return _someProp;
}
与仅使用相反:
public var someProp:String;
我意识到当您需要进一步处理或需要在属性更改时收到通知时,使用 getter/setter 会很有用,如下所示:
public function set someProp(value:String):void
{
_someProp = value;
_somePropChanged = true;
doSomethingElse();
}
但如果您不需要这个,那么有什么理由使用 getter/setter 而不是只使用公共 var 呢?
谢谢!!
Is there a benifit to using:
private var _someProp:String;
public function set someProp(value:String):void
{
_someProp = value;
}
public function get someProp():String
{
return _someProp;
}
As opposed to just using:
public var someProp:String;
I realise using getter/setter can be useful when you need to further processing or need to be notified of when the property is changed like so:
public function set someProp(value:String):void
{
_someProp = value;
_somePropChanged = true;
doSomethingElse();
}
But if you don't need this, then is there any reason to use getter/setter over just using a public var?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据您的语言,您应该更喜欢预先使用 getter/setter,因为如果您确实需要它们,则无法稍后引入它们(我正在看着您,Java)。
Depending on your language, you should prefer getter/setter up front because you can't introduce them later (I'm looking at you, Java) if it turns out you do need them.
这实际上在一定程度上取决于您所使用的语言/框架/工具包 -
但是,使用与版本控制和 API 兼容性相关的 getter 和 setter 通常有好处。这本身就是使用它们的一个非常有用的理由。
This really depends a bit on the language/framework/toolkit you are using -
However, there are often benefits when using getters and setters related to versioning and API compatibility. This can be a very useful reason to use them, all on its own.
如果不懂语言,这个问题确实无法回答。在大多数语言中,Getters 和 Setters 的成本更高,但它们可以为您带来以后的灵活性。在某些语言中,如果不更改调用者中的代码,则无法将 public 更改为 Getter/Setter,因为 use 语法发生了变化。但这对于 C# 来说不是问题,我主要是用 C# 编写的。
Getters 和 Setters 让您可以进行参数验证。它们可以让您延迟对象的创建,直到第一次引用。它们有很多优点,只要我从一开始就需要其中一个优点,我就会使用它们。
但我只在我立即需要 getter 和 setter 时,或者当我非常确定我需要灵活性时才使用它们。否则我认为它们是臃肿的。
This really can't be answered without knowing the language. Getters and Setters cost more in most languages, but they buy you flexibility down the road. In some languages you can't change a public to a Getter/Setter without changing the code in the callers because the use syntax changes. But this is not an issue with C#, which I what I write in mostly.
Getters and Setters let you do parameter validation. They let you delay creation of objects until first reference. They have a lot of advantages, and I use them whenever I need one of those advantages from the beginning.
But I use getters and setters ONLY when I need them right away, or when I'm pretty sure I'm going to need the flexibility. Otherwise I view them as bloat.
顺便说一句,您可以从公共 var 开始,如有必要,稍后在代码中将其转换为 getter / setter。取决于您使用的语言。
As a side note, you can start with a public var and if necessary convert it to a getter / setter later in code.. depending on the language you are using.
如果你的财产完全是愚蠢的,并且对班级的其他人没有副作用 - 那么无论如何只需将其公开为公共领域。
只是在许多情况下,您的属性将会产生副作用,因此您需要控制它们的设置和访问方式。
举一个简单的例子,如果您的公共变量未在构造函数中设置为某些值,会发生什么情况?您同意将其返回为 null 吗?或者您想将此变量设置为某个值而不是返回 null?这是一个值得使用自定义 getter 的简单示例。
If your property is totally dumb, and has no side effects on the rest of the class - then by all means just expose it as a public field.
Its just that in many cases your properties will have side effects, so you need to control how they are set and accessed.
As a trivial example, what happens if your public variable is not set to something in your constructor? Are you ok with this being returned as null? Or would you like to set this variable to something rather than return null? This is a simple example where a custom getter is worthwhile.
Getters 和 Setters 还可以让您更好地控制变量可以设置的值。
如果没有设置器,该值可能会被设置为零,并且可能会发生不好的事情。
Getters and Setters also give you more control over what values the variable can be set to.
Without the setter, the value could be set to zero and bad things could happen.