我有一个关于魔术方法getter和setter的问题。更好地使用setter和getter
我的问题是:什么更好(更快,更安全)?
P.S.这是ActionScript 3,但这个问题可以为PHP,JavaScript,C#等其他语言解答。
情况1
public class Test extends Sprite
{
private var _test : uint = 0;
public function Test()
{
start();
}
private function start() : void
{
trace(_test); ** Take the private var _test **
}
public function set test(value : uint) : void
{
_test = value;
start();
}
public function get test() : uint
{
return _test;
}
}
或案例2
public class Test extends Sprite
{
private var _test : uint = 0;
public function Test()
{
start();
}
private function start() : void
{
trace(test); ** difference here, take the public function test **
}
public function set test(value : uint) : void
{
_test = value;
start();
}
public function get test() : uint
{
return _test;
}
}
什么是最好的(最快)的方法?
谢谢!
真的没有想过这件事。我将使用getter和setter,所以我没有重复的代码来操作属性。 – 2012-07-06 07:36:44