1

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
IneedHelp
  • 1,630
  • 1
  • 27
  • 58
  • 1
    It might be better to solve it in a different way. It's usually good to strive to make encapsulated and decoupled classes. Post some code, and we might be able to suggest improvements :) – Jonatan Hedborg Jan 09 '12 at 15:31
  • I would've posted the code I'm working on, but it's a very complex loaders structure and I believe that in the end it would be irrelevant, instead I will try to outline what I'm looking for through some simple example code (added as an edit to question). Thank you, Jonatan! – IneedHelp Jan 09 '12 at 16:03

2 Answers2

2

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.

Emin A. Alekperov
  • 1,067
  • 1
  • 16
  • 26
1

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.

daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
  • Hello, Daniel. I am a bit confused mainly regarding point C). Isn't "publis static class" supposed to be "public static function"? Also, I thought that the keyword this isn't accepted in static functions. – IneedHelp Jan 09 '12 at 16:22
  • Is it possible I've messed it up :D. I was in a hurry, fixed, sorry :) – daniel.sedlacek Jan 09 '12 at 16:49
  • I've noticed your edit and then your comment on the next person who answered :D. Thanks for the info! – IneedHelp Jan 09 '12 at 16:53