Var bruce:Person = new Person(96.4, 1.72) Once you've imported Person, you can create instances of it and assign them to a variable with different height and weight values: var marty:Person = new Person(71, 1.76) The import statement follows the same name used for the package and includes the name of the class you want to include on the end: import classes.Person
This statement will need to be used within any class that belongs to a different package. Though the class is public, accessing it from anywhere outside of the package it belongs in will require the use of an import statement.
This can be done from anywhere that can access the Person class, which in this case is anywhere in the project because we've made the class public. Now that we've defined our new class, we can create instances of this class using the new keyword. Determine my BMI and return the result. We may want to be able to easily determine the BMI of each instance of Person that we create, so we should create a method for that: package classes Methods can belong to any namespace that you would expect properties to be able to belong to. Methods often return values and can accept arguments. Methods are used to hold logic that can be run when calling that method. Public function Person(height:Number, weight:Number) Constructors can accept arguments, which are typically used to pass in references to dependencies for that class or required values. It must be public and it does not return anything. The class constructor has the same name as the class itself. Each instance will have its own set of these properties.Ĭlass constructors (I believe this is what you're asking about).Ĭonstructors are used to hold logic that should be run as soon as an instance of your class is created. These properties can be accessed via any instance of Person that you create. Our Person class may enjoy the properties height and weight: package classes Properties are most commonly found clustered together at the top of your class. They may belong to one of a number of namespaces (including your own) and are used to hold values that belong to your class. Properties are defined using the var keyword. If you wanted to create a class called "Person" within the package classes, then we would end up with: package classesĬlasses can contain properties. Interfaces are an advanced topic thus I suggest you forget about these for now until your AS3 and OOP have evolved. If a base class is defined, then your new class will act as an extension to that class, inheriting all of the qualities of the base class. An internal class can only be seen by classes within the same package, whereas public classes can be seen from anywhere in the project. Do not confuse this with the package itself, which can contain many classes - each class just needs to have its own file with the same package statement.Ī class definition is made up of up to 5 parts: Within a package statement, you may insert one class. fla, then classes within that folder will need a package statement that reflects that: package classes For example, if you have a folder "classes" within the same directory as the project. The package statement reflects the location of the class relative to the. This gives us the first block, where you need to define the class. Okay, so firstly like you mentioned, a class must be wrapped by a package 1.