Flash basic Theory Class chapter II ActionScript 3.0 Animation Foundation Ⅰ

Source: Internet
Author: User
Tags definition class definition

Back to "flash Basic Theory Class-Catalog"

Class and object oriented programming

Classes (Class) and object-oriented (object oriented), for some readers may not have been contacted and some readers may have been used in as (or other languages) for many years, so that everyone can learn, I will briefly introduce these basics. Even OOP experts like 2 want to skim this section, because the working principle of as 3.0 does make a big difference. If you say you've never used a class, you're wrong, as long as you've written code in Flash, you've actually used the class. Class can be simply understood as an object, MovieClip is the class of the movie clip, and text boxes, movie clips, buttons, strings, and values have their own classes.

The basic two parts of a class: attributes (data or information), behavior (action or what it can do). property is used to hold information variables related to the class, and behavior (Behavior) refers to functions, and if a function is part of the class, then we call it a method.

A basic class:

Friends of Flash often know that we can create a component in the library, and use this component to create many instances on the stage. Like a component and an instance, a class is a template, and an object (like an instance) is a special representation of a class. Here's an example of a class:

package {
 public class MyClass {
  public var myProperty:Number = 100;
  public function myMethod() {
   trace("I am here");
  }
 }
}

Let's explain this code first. Here are some new knowledge for as 2 veteran too: package declaration. Package (Package), the role is to group related classes. Knowing this is enough, we no longer discuss it in depth, and the example in this book does not even use the package. Package this keyword and a pair of curly braces are required, we understand the default package, followed by the definition of the class.

Another change is that the classes in the AS3.0 have access to the keyword. Access keyword refers to a keyword that specifies whether other code can access the code. The public (common Class) keyword means that the class can be accessed by code from any external class. All the examples in this book have classes that are public. After studying as 3.0 in depth, we will find that not all classes are public, and even multiple classes, which are beyond the scope of this book.

We can see in this example that the name of this class is MyClass, followed by a pair of curly braces. There are two elements in this class, one is a variable named MyProperty, and the other is a function named MyMethod.

Package (Package)

Packages are primarily used to organize management classes. Packages are based on the directory path where the class resides and can be nested in multiple layers. The package name refers to a real-existence folder, with the "." are separated. For example, there is a class named Utils that exists in the folder com/friendsofed/makingthingsmove/(using a domain name as the package name is an unwritten rule to ensure that the package name is unique). This class is written as Com.friendsofed.makingthingsmove.Utils.

In as 2, use the entire package name to create a class, for example:

class com.friendsofed.makingthingsmove.Utils {
}

In as 3, the package name is written in the reputation of the package, the name of the class, such as:

package com.friendsofed.makingthingsmove{
 public class Utils {
 }
}

Imports (Import)

Imagine, every time you want to use the method of this class to input com.friendsofed.makingthingsmove.Utils, is too cumbersome too rigid. Don't worry, the import statement resolves the problem. In this example, the following sentence can be placed above the class definition in package: import com.friendsofed.makingthingsmove.Utils;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.