隐式vs显式getters / setters在AS3,哪些使用和为什么? [英] Implicit vs explicit getters/setters in AS3, which to use and why?

查看:187
本文介绍了隐式vs显式getters / setters在AS3,哪些使用和为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从AS3出现以来,我一直这样工作:

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;
}



我已避免使用AS3的隐式getter / setter部分,键入get ..和内容辅助将给我一个所有我的getters的列表,同样为我的设置器。

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!" );

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.

安装Flash Builder和伟大的新插件 SourceMate (这有助于得到一些有用的功能FDT是着名的FB)我意识到,当我使用SourceMate的生成getter和setter功能,它使用隐式路由自动设置我的代码:

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;
}



我认为这些SourceMate人必须知道他们在做什么,在AS3中编写工作流增强插件编码,所以现在我质疑我的方式。

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.

所以我的问题是:任何人都可以给我一个很好的理由为什么我应该放弃我明确的g / s的方式,开始使用隐式技术,并拥抱这些臭的小_underscores我的私人vars?

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?

推荐答案

说实话,我认为这是很多像缩进或大括号样式 - 其中重要性/乐于帮助匹配您的风格与您正在使用的任何代码库任何方法的任何固有的优势。

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?

// with getters
body.position.y += body.velocity.y * dt;

// without
body.getPosition().setY( body.getPosition().getY() + body.getVelocity.getY() * dt );



<如果需要,可以在稍后更改为getter / setter,而无需更改外部代码。你不必抢先为每个变量构建访问器;您可以等到您确定需要它们。

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.

这篇关于隐式vs显式getters / setters在AS3,哪些使用和为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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