Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Access Properties And Methods of a Class Without Extending or Instantiating?
    primarykey
    data
    text
    <p>Let's say I have a defined ClassA. ClassB extends ClassA and there's a Movie Clip instance on the stage linked to ClassB.</p> <p><strong>How would ClassC access the properties and methods of ClassB without extending ClassB or creating a new instance of ClassB?</strong></p> <p>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.</p> <p>Edit: Code example.</p> <pre><code>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. } } } </code></pre> <p>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.</p> <p><strong>Solved:</strong></p> <p>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.</p> <p>in ClassB:</p> <pre><code>private static var _instance:ClassB; public function ClassB() { // constructor _instance = this; } public static function get FUN_GetInstance():ClassB { return _instance; } </code></pre> <p>in ClassC:</p> <pre><code>private var MC_VariablesContainer:ClassB MC_VariablesContainer:MC_ClassB = ClassB.FUN_GetInstance </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload