如何ActionScript 3的类工作 [英] How Actionscript 3 Classes Work

查看:160
本文介绍了如何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.

在,这就是我不明白。看来你写的(公共)函数的类名的()

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

The package statement.

好了,首先像你提到的,一个类必须由 1 包裹。这给我们的第一个块,在这里你需要定义的类。

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

声明反映相对于的.fla 2 。例如,如果你有相同的目录项目的.fla内的文件夹类,那么该文件夹中的类将需要反映一个包语句:

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.

一个类的定义是由最高达5份:

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

  1. 的命名空间。一个类可以是内部公开。一个内部类只能通过类同一个包中可以看出,而公开类可以从任何地方看到项目
  2. 类的名称。
  3. 的基类(可选)。如果一个基类的定义,那么你的新类将作为一个扩展,类,继承所有的基类的品质。
  4. 的接口来实现(可选)。接口是一个高级的主题。因此,我建议你忘了这些,现在,直到你AS3和面向对象的进化。
  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.

类可以包含属性。属性是使用了 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.

我们的类可以享受的属性高度重量

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.

我们可能希望能够很容易地确定人员的每个实例的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);
        }
    }
}


实例


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

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

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


1 可以的地方类的包,可以在相同的。作为文件被称为外部。
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天全站免登陆