Question

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
Was it helpful?

Solution

A) Don't do it, think of better architecture that will respect encapsulation loose coupling.

B) If you have to do it, you can use static global register and make all stage instances (like your ClassB) assign to it, for instance:

in ClassB:

StaticRegister.assign(this);

in StaticRegister:

publis static var register : Array = [];

public static function assign(instance : Object) : void {
   array.push(instance);
}

And then get it out of there in similar fashion. But it's really dirty to do it like this.

C) If there is only one instance of ClassB you can give it static - singleton like - accessors:

In ClassB:

private static var instance : ClassB;  

publis static function getInstance() : ClassB {
  return instance;
 }

In Constructor:

public class ClassB() {
   instance = this;
}

More about complete singletons: www.actionscript.org or Google or Wikipedia.

OTHER TIPS

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.

This is one of the base principles of OOP. You can not get private and protected properties or methods. If you want to get them, you should make them public or internal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top