2011-04-12 108 views
0

我正在嘗試在AS3中創建基本功能。AS3 1120實例化時出錯

我有這樣一個類:

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

    } 
} 

只是一些getter和setter。

在我的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訪問undefinded財產時,我嘗試訪問計算。你有什麼想法?

謝謝。

+0

什麼的'setTaxes點(稅:號碼)'如果不使用給定的參數? – Kodiak 2011-04-12 12:58:56

+0

究竟是哪一個呢?你能從編譯器中粘貼完整的錯誤嗎? – Chris 2011-04-12 13:04:01

+1

你可以看到你的代碼在語法上沒有任何錯誤,但你對getter和setter應該做什麼有一些困惑,並且只是像getTaxes()返回grwage而不是稅的基本錯誤,setTaxes()和setNet()實際上不是制定者;如果它們用於計算和存儲結果,則應該調用calculateTaxes()和calculateNet()。另外,你的代碼示例會在你運行時跟蹤1000,NaN,NaN,NaN,因爲你得到的東西沒有被設置(只有grwage被設置)。 – 2011-04-12 13:38:18

回答

1

我想你在吸氣者和安裝者之間混合了一些東西。如果一個變量依賴於另一個變量,則必須將計算結果寫入其吸氣器中。這個變量不應該作爲私有成員存在,並且訪問它必須始終通過適當的getter來完成。

例如:

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

所以在這裏,你只需要一個二傳手(setGrwage),你可以刪除那些人,把計算的干將。

請注意,對於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; 
    } 
} 
+0

謝謝科迪亞克,我改變了我的班級。但是我仍然遇到實例化的問題。它說「1120訪問未發現財產計算」。 – Chrisissorry 2011-04-12 15:42:44

+0

您發佈的代碼是否在您的mxml中?您應該將代碼(從calculate.setGrwage開始)放入事件偵聽器(如creationComplete)中。 – Kodiak 2011-04-12 16:01:35

0

您的班級有多個問題。因此讓我們重新設計它
您遇到的主要問題是您在訪問它們之前沒有啓動變量,因爲它們有一個NULL值,Flash Player向您拋出該錯誤。 正如你可以在構造函數中看到的,我設置了一個默認值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 
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()

雖然你的課程可能不是以最好的方式書寫,但它在語法上是合理的。

嘗試在你的獲得者之前調用每個setter。我想你會解決你的問題。