AS3 中的隐式与显式 getter/setter,使用哪个以及为什么?
自从 AS3 出现以来,我一直这样工作:
private var loggy:String;
public function getLoggy ():String
{
return loggy;
}
public function setLoggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
this.loggy = loggy;
}
并且避免这样工作:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
_loggy = loggy;
}
我部分地避免使用 AS3 的隐式 getter/setter,这样我就可以开始输入“get..”,内容辅助会给我一个列表我所有的吸气剂,以及我的吸气剂。我也不喜欢代码中的下划线,这让我关闭了隐式路由。
另一个原因是我更喜欢这样的感觉:
whateverObject.setLoggy( "loggy's awesome new value!" );
。
whateverObject.loggy = "loggy's awesome new value!";
我觉得前者更好地反映了代码中实际发生的情况 我正在调用函数,而不是直接设置值。
安装 Flash Builder 和出色的新插件 SourceMate (这有助于将 FDT 著名的一些有用功能集成到 FB 中)我意识到,当我使用 SourceMate 的“生成 getter 和 setter”功能时,它会自动使用隐式路径设置我的代码:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// do whatever is needed to check to make sure loggy is an acceptable value
_loggy = loggy;
}
我认为这些 SourceMate 人员必须知道他们在做什么或者他们不会为 AS3 编码编写工作流程增强插件,所以现在我质疑我的方式。
所以我对你的问题是:任何人都可以给我一个充分的理由为什么我应该放弃我的显式 g/s 方式,开始使用隐式技术,并为我的私有变量拥抱那些臭烘烘的小_下划线?或者支持我这样做的理由?
Since the advent of AS3 I have been working like this:
private var loggy:String;
public function getLoggy ():String
{
return loggy;
}
public function setLoggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
this.loggy = loggy;
}
and have avoided working like this:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// checking to make sure loggy's new value is kosher etc...
_loggy = loggy;
}
I have avoided using AS3's implicit getters/setters partly so that I can just start typing "get.." and content assist will give me a list of all my getters, and likewise for my setters. I also dislike underscores in my code which turned me off the implicit route.
Another reason is that I prefer the feel of this:
whateverObject.setLoggy( "loggy's awesome new value!" );
to this:
whateverObject.loggy = "loggy's awesome new value!";
I feel that the former better reflects what is actually happening in the code.
I am calling functions, not setting values directly.
After installing Flash Builder and the great new plugin SourceMate (which helps to get some of the useful features that FDT is famous into FB) I realized that when I use SourceMate's "generate getters and setters" feature it automatically sets my code up using the implicit route:
private var _loggy:String;
public function get loggy ():String
{
return _loggy;
}
public function set loggy ( loggy:String ):void
{
// do whatever is needed to check to make sure loggy is an acceptable value
_loggy = loggy;
}
I figure that these SourceMate people must know what they are doing or they wouldn't be writing workflow enhancement plugins for coding in AS3, so now I am questioning my ways.
So my question to you is: Can anyone give me a good reason why I should give up my explicit g/s ways, start using the implicit technique, and embrace those stinky little _underscores for my private vars? Or back me up in my reasons for doing things the way that I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,我认为这很像缩进或大括号样式 - 将样式与您正在使用的任何代码库相匹配的重要性/有用性使这两种方法的任何“固有”优势都黯然失色。尽管如此,您更愿意在物理引擎中维护其中哪一个?
getter/setter 的另一个优点是,您始终可以最初将属性设置为简单的公共变量,然后在需要时将它们重构为 getter/setter,而无需更改外部代码。您不必为每个变量预先构建访问器;您可以等到您决定需要它们为止。
To be honest I think this is a lot like indenting or brace style - where the importance/helpfulness of matching your style to whatever codebase you're working with eclipses any "inherent" advantage to either approach. With that said though, which of these would you rather maintain in a physics engine?
Another advantage to getters/setters is that you can always make properties simple public variables initially, and refactor them into getters/setters later if needed, without changing external code. You don't have to preemptively build accessors for every variable; you can wait until you decide you need them.
我可以立即想到几个原因。
请记住,如果您所做的只是直接 get/set,那么仅暴露公共 var 并使用持有 var (_variable) 绕过 getter/setter 并不会造成太多损失。您稍后可以随时更改为隐式获取/设置,而无需更改类的外部接口。
I can think of a few reasons off the top of my head.
Keep in mind that if all you're doing is straight get/set, there isn't a whole lot lost by just exposing a public var and bypassing the getter/setter with the holding var (_variable). You can always change to an implicit get/set later without changing the classes external interface.
我不使用隐式 getter,因为 if 会强化重构和代码可读性。通常不太容易发现赋值是公共字段还是访问器方法。因此,如果您想跟踪错误,您可能会陷入令人讨厌的狩猎之中。
I don't use implicit getters because if hardens refactoring and code readability. It is usually not very easy to spot if assignment is to public field or it is accessor method. So if you want to track a bug you might fall into nasty hunting.