Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex 4 two classes in one file

Can I put two or more actionscript classes in one .as file like this:

//A.as
package classes {

    public class A {
        public function A() {
            var b:B = new B();
        }
    }
    internal class B {
        public function B() {
            trace("Hello");
        }
    }
}

It doesn't work in Flash Builder:

A file found in a source-path can not have more than one externally visible definition. classes:A; classes:B

If it possible, I'm going to ask next question.
Can I place two or more packages with multiple classes in one .as file?

like image 676
user578737 Avatar asked May 04 '11 18:05

user578737


People also ask

Can I define two classes in one Java file?

In Java, you can define multiple top level classes in a single file, providing that at most one of these is public (see JLS §7.6).

Is it good practice to have multiple classes in a file?

No. This is bad advice. Take the advice of the accepted answer. You should separate the object repository from the class you saving.


1 Answers

No and no. The following works:

//A.as

package classes {

    public class A {
        public function A() {
            var b:B = new B();
        }
    }

}
class B { // <--- Note the class is outside of the package definition.
    public function B() {
        trace("Hello");
    }
}

The class B is only visible to the class A - you cannot have more than one visible class in one file (exactly what the error message states). And you cannot have more than one package in a file.

like image 90
Tomasz Stanczak Avatar answered Oct 18 '22 03:10

Tomasz Stanczak