AS3 1120实例化时出错

时间:2011-04-12 12:26:41

标签: actionscript-3 class

我正在尝试在AS3中创建基本功能。

我有一个这样的课程:

package tax 
{
    public class Calculator
    {

        public function Calculator() 
        {
        }

        private var grwage:Number;
        private var taxes:Number;
        private var superannuation:Number;
        private var insurance:Number;
        private var net:Number;

        public function getGrwage():Number {
            return grwage;
        } 

        public function setGrwage(grwage:Number):void {
            this.grwage = grwage;
        }

        public function getTaxes():Number {
            return grwage;
        } 

        public function setTaxes(taxes:Number):void {
            this.taxes = grwage * 0.2;
        }

        public function getSup():Number {
            return superannuation;
        } 

        public function setSup(superannuation:Number):void {
            this.superannuation = superannuation * 0.05;
        }

        public function getIns():Number {
            return insurance;
        } 

        public function setIns(insurance:Number):void {
            this.insurance = insurance * 0.1;
        }

        public function getNet():Number {
            return net;
        } 

        public function setNet(net:Number):void {
            this.net = grwage - taxes - superannuation - insurance;
        }

    }
}

只是一些吸气剂和制定者。

在我的mxml文件中:

        import tax.Calculator;

                    public var calculate:Calculator = new Calculator();

        calculate.setGrwage(1000);
        trace(calculate.getTaxes());
        trace(calculate.getSup());
        trace(calculate.getIns());
        trace(calculate.getNet());

我收到错误1120当我尝试访问计算时访问未找到的属性。你有什么想法吗?

感谢。

3 个答案:

答案 0 :(得分:1)

我猜你们在吸气剂和制定者之间混淆了一些东西。如果变量依赖于另一个变量,则必须在其getter中编写计算。此变量不应作为私有成员存在,并且必须始终通过适当的getter来访问它。

例如:

public function getNet():Number {
    return getGrwage() - getTaxes() - getSup() - getIns();
}

所以在这里,你只需要一个setter(setGrwage),你可以删除其他的setter并将计算放在getter中。

请注意,AS3中的setter和getter更合适syntax

class GetSet 
{ 
    private var privateProperty:String; 

    public function get publicAccess():String 
    { 
        return privateProperty; 
    } 

    public function set publicAccess(setValue:String):void 
    { 
        privateProperty = setValue; 
    } 
}

答案 1 :(得分:0)

您班级有多个问题。所以我们重新设计它 你遇到的主要问题是你在访问它们之前没有启动变量,因为它们有一个NULL值,闪存播放器会把这个错误抛给你。 正如您在构造函数中看到的,我将默认值设置为0,以便它们永远不会为NULL。

package tax {
    public class Calculator{

        private var _grwage:Number;
        private var _taxes:Number;
        private var _superannuation:Number;
        private var _insurance:Number;

        public function Calculator() {
          this._grwage = 0;
          this._taxes = 0;
          this._superannuation = 0;
          this._insurance = 0;
        }

        public function get grwage():Number {
            return this._grwage;
        } 

        public function set grwage(val:Number):void {
            this._grwage = val;
        }

        public function get taxes():Number {
            return this._taxes;
        } 

        public function set taxes(val:Number):void {
            this._taxes = val * 0.2;
        }

        public function get superannuation():Number {
            return this._superannuation;
        } 

        public function set superannuation(val:Number):void {
            this._superannuation = val * 0.05;
        }

        public function get insurance():Number {
            return this._insurance;
        } 

        public function set insurance(val:Number):void {
            this._insurance = val* 0.1;
        }

        public function get net():Number {
            return this._grwage -  this._taxes -  this._superannuation -  this._insurance;
        }

    }
}


    import tax.Calculator;
    public var calculate:Calculator = new Calculator();
     //Now due to the fact we are using true setter/getter methods.
    calculate.grwage = 1000;
    trace( calculate.taxes); // should be 0 as we are defaulting to the initial value
    trace( calculate.superannuation );// should be 0 as we are defaulting to the initial value
    trace( calculate.insurance );// should be 0 as we are defaulting to the initial value
    trace( calculate.net );// should be 1000 as we are defaulting to the initial values of everything but grwage

答案 2 :(得分:0)

这里的答案都很好,但他们都错过了主要观点:

您在设置之前尝试获取变量。

import tax.Calculator;

public var calculate:Calculator = new Calculator();

calculate.setGrwage(1000);
trace(calculate.getTaxes());
trace(calculate.getSup());
trace(calculate.getIns());
trace(calculate.getNet());

这会引发错误(由于您设置课程的方式),因为您在尝试使用它们之前从未调用setSup()setIns()setNet()

虽然你的课可能不是以最好的方式写的,但它在语法上是合理的。

尝试在getter之前调用每个setter。我想你会解决你的问题。