Overriding a public variable with set - actionscript-3

I have a base class like this
class Base {
public var space:Number;
}
which gets extended by
class Desc extends Base {
override public function set space( space:Number ):void {
//code
}
}
This doesn't compile. Say, you don't have control of the base class, what ways is there implement the same thing?
The obvious is create a function setSpace(), but this object is being embedded in an already existing system that use the public space.
Thanks in advance.

Your base class should be defined that way:
class Base {
// Don't use public variables. Make them private and define setters and getters
private var space_:Number;
public function get space():Number {
return space_;
}
public function set space(v:Number):void {
if (space_ === v) return;
space_ = v;
}
}
And then it can be overriden that way:
class Desc extends Base {
override public function set space( space:Number ):void {
//code
}
}
Edit:
I missed the part where you say you don't have control over the base class. In that case, hope that space is defined as a getter/setter (it should be if the class is implemented properly). If not, you'll indeed have to use a function such as getSpace and setSpace.

From Adobe:
Class member variables that are declared using the var keyword cannot
be overridden in a subclass.
Using a setter is not an option in your case. If you need to preserve the interface, use composition instead of inheritance:
class Desc {
private var _base:Base;
public function set space(space:Number):void {
//code
this._base.space = space;
}
}

Related

AS3 - how to extend this function

This is first class with "gordz()" function
public class Model extends Object implements IModel
{
public static function gordz() : void
{
newobject = gallas.pop();
}
}
Now i try to override the function but i still want that old code is executed... How can i extend this function correctly?
public class LOL extends Model
{
override public static function gordz() : void
{
... //New code + execute old code
}
}
Neither super
You cannot use the super statement in a static method.
nor override
You cannot use the override attribute on any of the following:
[...]
Static methods
can be used in a static method.
Whatever you are trying to do should be accomplished in a different way.

How to Override Constants in ActionScript 3

I have the two following classes:
public class Parent{
static internal const _name:String = "Parent";
public function get name():String{
return _name;
}
}
public class Child{
static internal const _name:String = "Child";
}
If I create an instance of class Child and call its name() getter, since it will call the name() method it inherits from Parent, it returns "Parent". I could, of course, override the name() method:
public class Child{
static internal const _name:String = "Child";
override public function get name():String{
return _name;
}
}
That returns "Child". However, it seems silly to have to copy the exact same code of the method from the parent. Is there any simpler way to do this?
I would take a different approach by making the "name" property a requirement for the parent's constructor:
public class Parent
{
static internal var _name : String;
public function Parent(name : String = "Parent") {
_name = name;
}
public function get name() : String {
return _name;
}
}
Child Class:
public class Child extends Parent
{
public function Child() {
super("Child");
}
}
Firstly, you cannot override static methods or properties - they are not inherited, so no override for them.
Secondly, if you declared a constant to be of a complex type, it is not really a constant. I.e. if it is an object, then you can change its keys / values, if it is an array, you can add / remove members and so on.
But the desire to make this functionality more generic is understandable. So, what I'd do:
Have some property outside both parent and child, let say in class X, or package Y. Let it be package Y. So, you'd create a dictionary in package Y, let it be Y.names and in your name getter you'd do:
import Y.names;
. . .
public function get name() {
return names[(this as Object).constructor];
}
your names variable would be:
package Y {
public var names:Dictionary = generateNames();
internal function generateNames():Dictionary {
var result:Dictionary = new Dictionary();
result[ChildClass] = "child";
result[ParentClass] = "parent";
. . .
return result;
}
}
This way it would be sufficient to only implement name getter in super-class, and all inheriting classes will be able to use super-class code as is, no need to change anything. However, this means that some (maybe important) information pertaining to this class will be stored elsewhere (may be difficult to find, this is not the common way people program in AS3).
your implementation of get name should look like this, then the getter is one and each of the new classes needs to have it's own public static var _name defined:
//in the base class
public function get name():String
{
var _sName:String;
if ((this as Object).constructor._name)
{
_sName = (this as Object).constructor._name;
}
else
{
try
{
var o:Object = getSuperClass(this);
while (o)
{
if (o._name)
{
_sName = o._name;
break;
}
o = getSuperClass(o);
}
}
catch (e:*)
{}
}
return _sName;
}
//as found here: http://www.actionscriptdeveloper.co.uk/getting-the-class-of-an-object-in-as3/
public static function getSuperClass(o: Object): Object
{
var n: String = getQualifiedSuperclassName(o);
if (n == null)
return(null);
return getDefinitionByName(n);
}
the static members can be accessed only via class reference which we can get from constructor object, "this" will point to the current class in the inheritance chain so you can call this in parent class and it will point to a Child in a Child class.
[EDIT]
I've modified it so it tests for existance of the public static property _name if not found on "this" instance then in a loop the parent class is checked until one is found - like inheritance:)
I'm using this feature to create clone method: constructor as helper in clone method implementation
best regards
Why don't you store such a constant within a corresponding function instead of declaring an inaccessible constant?
class Parent {
...
public function get name():String { return 'Parent'; }
}
class Child extends Parent {
...
override public function get name():String { return 'Child'; }
}
By the way, if your Parent class is a descendant of DisplayObject, you should be careful with name property, as it's needed sometimes by operating code, e.g. getChildByName().
I have found something that seems to work. Any feedback is greatly appreciated:
public class Parent{
prototype._name = "Parent";
public function get name():String{
return this["_name"];
}
}
public class Child{
prototype._name = "Child";
}

Inheriting a class that uses a Has-A relationship and replacing the owned class with a subclass?

I am using ActionScript3, I am writing a class that extends Bitmap, I want to have all of Bitmap's behaviors and I also want to treat to replace its bitmapData with a subclass of bitmapData that offers more flexibility.
So basically what I have is(metaphor):
Class Gunman
{
//Has-A
public var pistol : Gun;
public function Gunman(gun : Gun)
{
this.pistol = gun;
}
//methods
public function shoot():void ..
{
pistol.fire();
}
}
Class Gun
{
//constructor omitted cause it is unnecessary for this example
public function fire():void ..
}
Now I extend these two classes.
Class Automatic extends Pistol
{
override public function fire():void
{
super.fire(); super.fire(); super.fire();
}
}
Class NavySeal extends Gunman
{
public function Enforcer(auto : Automatic)
{
super(auto);
}
}
The problem is that people can still easily do this:
var eliteSoldier : NaveSeal = new Enforce(new Automatic());
eliteSoldier.pistol = new Gun();
And this will be bad. Is there any way I prevent this?
I cannot change 'Gunman' cause it is part of the API.
If you are trying to prevent from changing public property public var pistol:Gun; than you can't if you haven't got the control over the class that has defined it. It is a bad design, it should be the getter/setter instead. Then you would be able to get and test what users are passing etc.
best regards

AS3 how do you send vars to the parent constructor of an inherited class?

That title probably made no sense, so I'll try to explain.
Let's say I create a class in AS3, any class with some basic vars, and those vars are set through that classes constructor
so..
var class = new basicClass(var1, var2);
Now let's say I want to create a new class which inherits the basicClass's vars/methods etc, so new class has something like
public class betterClass extends basicClass
and maybe that also has some vars that are set via it's constructor.
Now what I'm confused about is, how do you set the parent classes (basicClass) vars or do anything through it's constructor by creating betterClass?
If basicClass has a constructor which expects values, then how do you set that up, by doing...
var class2 = new betterClass(var3, var4)
because var3 and var4 are for the constructor of betterClass, I don't see how an instance of basicClass is setup.
I know this is pretty basic stuff, it's just I've never needed to use inheritance before.
So you have got BasicClass, its constructor may look like this:
public function BasicClass(var1:*, var2:*)
{
//do some stuff with var1 and var2
}
//you can extend some metods too
public function niceFunction():void
{
trace('Oh! Hello!');
}
Now you can create BetterClass:
public function BetterClass(var3:*, var4:*)
{
super(var3, var4);
//this calls constructor of BasicClass
}
override public function niceFunction():void
{
super.niceFunction();
trace('Nice to meet you.');
//This will trace: Oh! Hello! Nice to meet you.
}
You can call the parent class's constructor from your extending class using the 'super' keyword'
public betterClass(){
super(constuctorParam1,constructorParam2);// this calls the constructor of the class this class extended
}
Here is more detailed info
http://ntt.cc/2009/07/26/beginning-actionscript-3-super-this-setter-getter.html
Sometimes, you have different signatures
package {
public class A {
protected var _a:String, _b:String;
public function A(a:String, b:String) {
_a = a;
_b = b;
}
}
}
package {
public class B extends A {
private var _c:Number;
public function B(c:Number) {
super('', '');
_c = c;
}
}
}
You have to be aware of the different arguments and their meaning. Also, provide useful defaults if possible.
Just some additional input.

AS3 override public function both are called?

I am a little confused by this concept.
If I override a public function in a base class, I would have thought that this override function is called and the original is ignored? though this doesn't seem to be the case...
public class AbstractScreen extends Sprite
{
public function AbstractScreen()
{
}
public function updateLanguage():void
{
trace("WARNING: need to override public function updateLanguage()");
}
}
public class Start extends AbstractScreen
{
override public function updateLanguage():void
{
title.text = _model.language.start
title.setTextFormat(titleFormat);
}
}
public class ViewManager extends Sprite
{
private var _model:Model;
private var _screens:Array;
public function ViewManager(model:Model)
{
_model = model;
_model.addEventListener(AppEvent.INIT_VIEW, initViews);
_model.addEventListener(AppEvent.UPDATE_VIEW, updateView);
_model.addEventListener(AppEvent.CHANGED_LANGUAGE, changeLanguage);
}
private function initViews(e:AppEvent):void
{
trace("View Manager: initViews");
_screens = new Array(new Start(_model), new TakePhoto(_model));
dispatchEvent(new ViewEvent(ViewEvent.VIEW_READY));
}
private function changeLanguage(e:AppEvent):void
{
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
}
}
If my model dispatches a CHANGED_LANGUAGE event, the text in the views gets updated, But I also get a trace of "WARNING: need to override public function updateLanguage()" What could I be doing wrong?
You are right, this should not call the base class function. Are you sure there is no call to
super.initLanguage()
within your override?
Most IDE's add this call in the function body automatically, if they create an override for you.
EDIT
From your edit I see your are iterating over two objects of the types Start and TakePhoto. I assume TakePhoto is also derived from AbstractScreen and the trace may be comming from this one.
Also I suggest to use the abstract base class in your iteration.
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}