2011-07-13 31 views
4

我有一個問題給我的錯誤「屬性爲只讀」,就像這樣:與公共的getter內部setter不起作用

public function get aVar():int{ return _aVar; } 
internal function set aVar(value:int):void { this._aVar = value; } 

我已經使用的一種變通方法:

public function get aVar():int{ return _aVar; } 
internal function setAVar(value:int):void { this._aVar = value; } 

這似乎是在AS3中的錯誤,或者也許我失去了一些東西?有誰知道更好的解決方法?

感謝

+0

我感覺到你的痛苦。我最近問同樣的事情。 http://stackoverflow.com/questions/5465793/actionscript-read-only-property-and-private-set-method解決方案是使用你自己的命名空間。 – TheDarkIn1978

回答

1

據我所知getter和setter必須是相同的,所以無論是公共或內部。

我目前無法找到的文檔來支持這一行動,雖然我已經在過去試圖混合的公共和私人的時候也有類似的錯誤。

+0

好吧,我沒有意識到這一點,會有趣的看到那些文檔@shanethehat –

+0

檢查TheDark1978上面的評論,他鏈接到的問題包含錯誤報告的鏈接。 – shanethehat

5

getter和setter必須具有相同的訪問類型,這是不是一個錯誤。

+2

雖然這是真的,但它不是一個真正的解決方案。 –

-1

由於在AS3沒有超載,不能在命名空間方面的任何含糊之處。訪問者被視爲屬性,因此,任何財產,就不能受保護和公衆。在你的情況的解決方案,也許,就是讓變量訪問器設計的把手保護(或內部),同時吸氣 - 公衆(和沒有setter)。

//吸氣劑publilc和setter是內部

public string EmployeeCode 
{ 
     // getter is publilc and setter is internal 
     public string EmployeeCode 
     { 
      get 
      { 
      return _employeeCode; 
      } 

      internal set 
      { 
      _employeeCode = value; 
      } 
     } 
} 

有一兩件事要記住的是,你不能爲兩個getter和setter的同時指定訪問修飾符。另一個將始終採用來自財產的默認值。然而這對我們無關緊要,因爲我們可以通過靈活性來實現任何組合。

+1

這不是有效的actionscript語法。 – Brian

2

問題已經到來之前,我已經提到的解決方法in my blog。問題在於Flex的綁定工作方式,因爲它需要在綁定事件分派之前比較值(默認情況下)。然而,解決方法很簡單,但由於額外的代碼而稍微令人討厭:

// Internal Variable 
private var __state:String = "someState"; 

// Bindable read-only property 
[Bindable(event="stateChanged")] 
public function get state():String 
{ 
    return this._state; 
} 

// Internal Getter-Setter 
protected function get _state():String 
{ 
    return this._state; 
} 

protected function set _state(value:String):void 
{ 
    this.__state = value; 
    dispatchEvent(new Event("stateChanged")); 
}