隐VS在AS3明确的getter / setter方法​​,而使用,为什么? [英] Implicit vs explicit getters/setters in AS3, which to use and why?

查看:134
本文介绍了隐VS在AS3明确的getter / setter方法​​,而使用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于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方法​​部分,这样我就可以开始输入得..和内容辅助会给我所有我干将的列表,同样为我制定者。我也喜欢在我的code这把我赶走的隐含路线下划线。

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.

还有一个原因就是我preFER这种感觉:

Another reason is that I prefer the feel of this:

whateverObject.setLoggy( "loggy's awesome new value!" );

这样:

whateverObject.loggy = "loggy's awesome new value!";

我觉得前者更好地反映实际发生的事情,在code。 我打电话的功能,而不是直接设定值。

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功能,它会自动将我的code起来使用隐式路线:

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为我的私人瓦尔?还是支持我在我的理由做的事情,我做的方法是什么?

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?

推荐答案

说实话,我觉得这是很多喜欢缩进或括号的风格 - 在这里你的风格相匹配的任何$ C $的重要性/乐于助人CBase的你与日食工作的任何先天优势,这两种方法。随着中说,虽然,这其中,你会在一个物理引擎,而维持?

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方法​​是,你总是可以使性能简单的公共变量开始,并重构他们进入以后,如果需要的getter / setter方法​​,在不改变外部code。你不必为preemptively建立访问的每一个变量;你可以等待,直到你决定你需要他们。

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在AS3明确的getter / setter方法​​,而使用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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