Archive for May, 2006

Extending Actionscript implicit getter & setters.

Here’s something i only just noticed. If you overwrite either a getter or setter in a subclass of a class with both, but don’t provide a hook for the other one, then the property will become either read or write only. For example:

MyClass{
var _i:Number
public function get i ():Number{
return _i
}
public function set i(num:Number):Void{
this._i=num
}
}

Will produce the following expected behaviour:

var myClass = new MyClass()
myClass.i=10
trace(myClass.i) //traces 10

In the subclass we overwrite the setter with some new behaviour:

mySubClass extends MyClass{
public function set i(num:Number):Void{
this._i=num*2
}
}

Will produce the following not so expected behaviour:

var myClass = new MySubClass()
myClass.i=10
trace(myClass.i) //traces undefined

Which means the property i has become write only. To keep the property read and write you need to copy the getter into the sub class:

mySubClass extends MyClass{
public function get i ():Number{
return _i
}
public function set i(num:Number):Void{
this._i=num*2
}
}

And you then get the following:

var myClass = new MyClass()
myClass.i=10
trace(myClass.i) //traces 20

I know it makes a kind of sense, but it does mean you have to be careful you’re not changing the signature of your subclasses, as they dont behave exactly as you might expect.

3 Comments


Nice design blog.

Just a quick one to point out webcreme. It’s a great design blog with hardly any text, just large thumbnails linking to some beautifully designed websites. The emphasis seems to be on sites that work too which is always good news. Skip intro need not apply.

1 Comment



  • SetPageWidth