Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

class - How Actionscript 3 Classes Work

I need a little help understanding how classes work in Actionscript 3. I understand you start with "package" and why and then go to import any necessary libraries, as well as then naming the class and stating if it's public/private and extends anything.

After that is what I don't understand. It seems you write "(public) function class name()

I don't understand why you do this and what goes in the curly brackets.

I've probably missed a bit of earlier reading because I've done a little reading but I can't seem to get it.

Could someone try explain it to me? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ActionScript 3 Classes

The package statement.

Okay, so firstly like you mentioned, a class must be wrapped by a package1. This gives us the first block, where you need to define the class.

package
{
    // Your class here.
}

The package statement reflects the location of the class relative to the .fla2. For example, if you have a folder "classes" within the same directory as the project .fla, then classes within that folder will need a package statement that reflects that:

package classes
{
    // Your class here.
}


Defining the class.

Within a package statement, you may insert one class. 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.

A class definition is made up of up to 5 parts:

  1. The namespace. A class can be internal or public. An internal class can only be seen by classes within the same package, whereas public classes can be seen from anywhere in the project.
  2. The class name.
  3. A base class (optional). 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.
  4. An interface to implement (optional). Interfaces are an advanced topic thus I suggest you forget about these for now until your AS3 and OOP have evolved.

If you wanted to create a class called "Person" within the package classes, then we would end up with:

package classes
{
    public class Person
    {
        // Class qualities here.
    }
}


Properties.

Classes can contain properties. Properties are defined using the var keyword. 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 most commonly found clustered together at the top of your class.

Our Person class may enjoy the properties height and weight:

package classes
{
    public class Person
    {
        // Properties.
        public var height:Number = 1.70;
        public var weight:Number = 67.5;
    }
}

These properties can be accessed via any instance of Person that you create. Each instance will have its own set of these properties.


Class constructors (I believe this is what you're asking about).

Constructors are used to hold logic that should be run as soon as an instance of your class is created. The class constructor has the same name as the class itself. It must be public and it does not return anything. Constructors can accept arguments, which are typically used to pass in references to dependencies for that class or required values.

package classes
{
    public class Person
    {
        // Properties.
        public var height:Number = 1.70;
        public var weight:Number = 67.5;

        // Constructor.
        public function Person(height:Number, weight:Number)
        {
            this.height = height;
            this.weight = weight;
        }
    }
}


Methods.

Methods are used to hold logic that can be run when calling that method. Methods often return values and can accept arguments. Methods can belong to any namespace that you would expect properties to be able to belong to.

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
{
    public class Person
    {
        // Properties.
        public var height:Number = 170;
        public var weight:Number = 65.5;

        // Constructor.
        public function Person(height:Number, weight:Number)
        {
            this.height = height;
            this.weight = weight;
        }

        // Determine my BMI and return the result.
        public function getBMI():Number
        {
            return weight / (height * height);
        }
    }
}


Instances.

Now that we've defined our new class, we can create instances of this class using the new keyword. 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.

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 statement will need to be used within any class that belongs to a different package. 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;

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);
var bruce:Person = new Person(96.4, 1.72);

We can then obtain the BMI for each person using their getBMI() method:

trace(marty.getBMI()); // 22.9
trace(bruce.getBMI()); // 32.6

1 You can place classes outside of a package which can be referred to in the same .as file.
2 You can add more source paths, and packages can be relative to that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...