Actionscript 3 类的工作原理 [英] How Actionscript 3 Classes Work

查看:30
本文介绍了Actionscript 3 类的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一点帮助来理解类在 Actionscript 3 中是如何工作的.我知道您从包"开始,然后导入任何必要的库,然后命名类并说明它是否是公共/私有的和扩展任何东西.

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.

之后就是我不明白的地方.看来你写了"(public) function class name()

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.

推荐答案

ActionScript 3 Classes

package 声明.

ActionScript 3 Classes

The package statement.

好的,首先就像你提到的,一个类必须被一个 package1 包裹.这给了我们第一个块,您需要在其中定义类.

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.
}

package 语句反映了类相对于 .fla2 的位置.例如,如果您在与项目 .fla 相同的目录中有一个文件夹classes",那么该文件夹中的类将需要一个反映这一点的包语句:

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.
}


定义类.

在一个包语句中,你可以插入一个类.不要将此与包本身混淆,包本身可以包含许多类 - 每个类只需要拥有自己的文件,其中包含相同的包语句.

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.

一个类定义最多由 5 个部分组成:

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

  1. 命名空间.一个类可以是internalpublic.internal 类只能被同一个包中的类看到,而 public 类可以在项目的任何地方看到.
  2. 班级名称.
  3. 基类(可选).如果定义了基类,那么您的新类将作为该类的扩展,继承基类的所有特性.
  4. 要实现的接口(可选).接口是一个高级主题,因此我建议您暂时忘记这些,直到您的 AS3 和 OOP 发展为止.
  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.

如果您想在 classes 包中创建一个名为Person"的类,那么我们最终会得到:

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.
    }
}


属性.

类可以包含属性.属性是使用 var 关键字定义的.它们可能属于多个命名空间之一(包括您自己的),并用于保存属于您的类的值.属性通常聚集在类的顶部.

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.

我们的Person 类可能会使用heightweight 属性:

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;
    }
}

可以通过您创建的任何 Person 实例访问这些属性.每个实例都有自己的一组这些属性.

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).

构造函数用于保存应该在创建类的实例后立即运行的逻辑.类构造函数与类本身同名.它必须是 public 并且不返回任何内容.构造函数可以接受参数,这些参数通常用于传递对该类或所需值的依赖项的引用.

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 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.

我们可能希望能够轻松确定我们创建的每个 Person 实例的 BMI,因此我们应该为此创建一个方法:

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);
        }
    }
}


实例.

现在我们已经定义了我们的新类,我们可以使用 new 关键字创建这个类的实例.这可以从任何可以访问 Person 类的地方完成,在本例中是项目中的任何地方,因为我们已经将类设为 public.

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.

尽管该类是public,但从它所属的包之外的任何地方访问它都需要使用import 语句.需要在属于不同包的任何类中使用此语句.import 语句遵循与 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 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;

导入Person 后,您可以创建它的实例并将它们分配给具有不同heightweight 值的变量:

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);

然后我们可以使用他们的 getBMI() 方法获得每个人的 BMI:

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

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

<小时>

1可以将类放在可以在同一个 .as 文件中引用的包之外.
2 你可以添加更多的源路径,包可以相对.


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.

这篇关于Actionscript 3 类的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆