我正在嘗試在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財產時,我嘗試訪問計算。你有什麼想法?
謝謝。
什麼的'setTaxes點(稅:號碼)'如果不使用給定的參數? – Kodiak 2011-04-12 12:58:56
究竟是哪一個呢?你能從編譯器中粘貼完整的錯誤嗎? – Chris 2011-04-12 13:04:01
你可以看到你的代碼在語法上沒有任何錯誤,但你對getter和setter應該做什麼有一些困惑,並且只是像getTaxes()返回grwage而不是稅的基本錯誤,setTaxes()和setNet()實際上不是制定者;如果它們用於計算和存儲結果,則應該調用calculateTaxes()和calculateNet()。另外,你的代碼示例會在你運行時跟蹤1000,NaN,NaN,NaN,因爲你得到的東西沒有被設置(只有grwage被設置)。 – 2011-04-12 13:38:18