views:

52

answers:

3

So, how do we write a class over several files in action script 3?

In C# there's the "partial" keyword.

In C++ it's natural (you just "#include ..." all files).

In Flex 3, in a component, you add this tag: <mx:Script source="myfile.as"/>.

How do I split the following class into several files;

package package_path
{
    public class cSplitMeClass
    {
        public function cSplitMeClass()
        {
        }

        public function doX():void
        {
            // ....
        }

        public function doY():void
        {
            // ....
        }
    }
}

For example I want to have the doX() and doY() functions implemented in another ".as" file.

Can I do this?

And please, don't tell me something like "a good practice is to have them in one file" :)

A: 

It's good practice, nothing wrong with that.

There's the import keyword. Example:

import Class_A.as;
import Class_B.as;

Now of course in order to use them, you need to declare them, preferably in your constructor.

public function Class_C()
{
    //Create a new instance of Class_A
    var objA:Object = new Class_A("parameter one", "parameter two");
    //Create a new instance of Class_B
    var objA:Object = new Class_B("parameter one", "parameter two");
}

Of course it all depends on how you're going to make this work. I would also suggest you use a Main class from where you could run your code. I guess you already knew that though.

Good luck.

Christopher Richa
Not understood. I'm not sure you understood what I was asking. I know how to "import" other classes. I don't know, and want to know, how I can write one class over several ".as" files!
Poni
+2  A: 

As per your request, I'm sparing you the "best practices lecture". So, I'll just say there's an include directive in AS 3.0, which could help you out here.

Basically, you can do:

package package_path
{
    public class cSplitMeClass
    {
        public function cSplitMeClass()
        {
        }

        include "the_file_where_doX_and_doY_live.as"
     }
}

And then in "the_file_where_doX_and_doY_live.as"

    public function doX():void
    {
        // ....
    }

    public function doY():void
    {
        // ....
    }
Juan Pablo Califano
Yup, that's it! So simple, yet so "complicated" to find. Typical Adobe's cryptographic.. (: Thank you!
Poni
A: 

You could do it with inheritance:

// file: cSplitMe1.as

class cSplitMe1 {

   function doX() {
       // ...
   }

// file: cSplitMe2.as

class cSplitMe2 extends cSplitMe1 {

   function doY() {
       // ...
   }

// file: cSplitMe.as

class cSplitMe extends cSplitMe2 {

   function cSplitMe() {
       doX();
       doY();
   }
}
davr
This is actually best practices if you divide up the classes in a way that makes sense. Flex frameworks does that for example. Now if you are just doing it because you want to dump a ton of code into a single class, and you split it in ways that don't make sense...well that's bad.
davr