Ols*_*dev 4 flash compiler-errors actionscript-3
是否可以拥有一个拥有公共getter和受保护的setter的属性?
我有以下代码:
public class Mob extends Sprite {
// snip
private var _health:Number; // tried making this protected, didn't work
public function get health():Number { return _health; }
protected function set health(value:Number):void {
_health = value;
}
// snip
public function takeDamage(amount:Number, type:DamageType, ... additionalAmountAndTypePairs):void {
var dmg:Number = 0;
// snip
var h:Number = this.health; // 1178: Attempted access of inaccessible property health through a reference with static type components.mobs:Mob.
this.health = h - dmg; // 1059: Property is read-only.
}
}
Run Code Online (Sandbox Code Playgroud)
我确实有,this.health -= dmg;但我把它拆分出来以获得有关编译器错误的更多细节.
我不明白该属性如何在同一个类中被认为是只读的.我也不明白它是如何无法进入的.
如果我将支持字段,getter和setter全部保护起来,它会编译,但这不是我想要的结果; 我需要外在可读的健康.
不,访问者必须具有彼此相同的权限级别.您可以拥有公共get set函数,然后拥有受保护的setHealth,getHealth函数对.如果您愿意,可以将其反转,但关键是您有一组方法可以在公共权限上访问,另一组方法可以在受保护的权限级别访问.
| 归档时间: |
|
| 查看次数: |
665 次 |
| 最近记录: |