【问题标题】:Better working with setter and getter更好地使用 setter 和 getter
【发布时间】:2012-07-06 15:08:06
【问题描述】:

我有一个关于 getter 和 setter 魔法方法的问题。

我的问题是:什么更好(更快、更安全)?

附:这是 ActionScript 3,但也可以针对 PHP、JavaScript、C# 等其他语言回答这个问题。

案例一

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

最好(最快)的方法是什么?

谢谢!

【问题讨论】:

    标签: actionscript-3 oop setter getter


    【解决方案1】:

    您已经完成了编写自己的测试用例以了解自己的方式的 90%。

    Getter 和 setter 旨在添加对设置或检索属性时发生的情况的控制,或者创建只读或只写属性。

    这些好处大大超过了任何可能的性能差异。


    至于那些性能差异,这里是一个测试环境:

    // Test property.
    var _test:uint = 0;
    function get test():uint{ return _test; }
    function set test(value:uint):void{ _test = value; }
    
    // Direct access test.
    function directTest(amt:int):Number
    {
        // Directly accessing the property.
        var t:Number = getTimer();
        for(var i:int = 0; i < amt; i++)
        {
            var temp:uint = _test;
            _test = i;
        }
    
        return getTimer() - t;
    }
    
    
    // Getter/setter test.
    function viaTest(amt:int):Number
    {
        // Via getter/setter.
        var t:Number = getTimer();
        for(var i:int = 0; i < amt; i++)
        {
            var temp:uint = test;
            test = i;
        }
    
        return getTimer() - t;
    }
    

    快速演示如何使用它:

    trace("Direct: " + directTest(1000000));
    trace("Getter/Setter: " + viaTest(1000000));
    

    以及我得到的一些结果:

    Amount      Direct      Get/Set
    1000        0           0
    5000        0           0
    20,000      0           2
    150,000     1           14
    500,000     2           46
    2,000,000   10          184
    10,000,000  47          921
    

    【讨论】:

    • 如果我设置和获取每秒 100 次这样的属性呢?
    • @Bondye,哇每秒 100 次?!在这里小心你可能会为你的 CPU 干杯。
    • @Bondye 我添加了一些实际数据供您观赏。从本质上讲,即使在访问该属性 1000 万次时,这两个选项的查看时间也不到一秒。
    • @MartyWallace 谢谢!
    【解决方案2】:

    我总是喜欢在声明它们的同一类的方法中使用 getter 和 setter(尽管我可以使用 privare 变量)。

    Getter 和 setter 隐藏了当您必须设置或获取类属性时所需的逻辑,因此您可以在 setter/getter 中修改“您实际执行的操作”,而不必担心对客户端方法的影响。

    它们还避免重复代码。

    但这只是我的观点... :)

    【讨论】:

    • 真的没想到。我将使用 getter 和 setter,因此我不需要重复代码来操作属性。
    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2016-02-19
    • 2011-11-09
    • 2012-01-07
    • 2011-03-02
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多