Let's say I have a defined ClassA. ClassB extends ClassA and there's a Movie Clip instance on the stage linked to ClassB.
How would ClassC access the properties and methods of ClassB without extending ClassB or creating a new instance of ClassB?
The way I'm currently doing it is by referencing the stage instance linked to ClassB and then using the dot syntax to access ClassB instance variables, and this works only if the the accessed variables are public or internal, depending on what package ClassC is part of. I would like to know if there's a better, cleaner way to accomplish this.
Edit: Code example.
package com.core.stage.classes {
import flash.display.MovieClip;
public class ClassA extends MovieClip {
public var classAvar:uint = 0;
public function ClassA() {
}
protected function classAfunction(funPar:uint):void {
classAvar = 2 * funPar;
}
}
}
package com.core.stage.classes {
import com.core.stage.classes.ClassA;
public class ClassB extends ClassA {
public function ClassB() {
classAfunction(10);
}
}
}
package com.core.stage.classes {
import flash.display.MovieClip;
public class ClassC extends MovieClip {
private var classBreference:*;
public function ClassC() {
classBreference = Object(parent);
trace(classBreference.classAvar); // Outputs 20.
}
}
}
So what I basically want to know is if there's a better way to get the value of classAvar (which was declared in ClassA, got a value after calling the method in ClassB) while working in ClassC.
Solved:
Ok, after some research and an idea I got from daniel.sedlacek, it seems that I have found the solution that best suits my needs.
in ClassB:
private static var _instance:ClassB;
public function ClassB() { // constructor
_instance = this;
}
public static function get FUN_GetInstance():ClassB {
return _instance;
}
in ClassC:
private var MC_VariablesContainer:ClassB
MC_VariablesContainer:MC_ClassB = ClassB.FUN_GetInstance