Class and Object-Oriented Programming
Class and object-oriented (Object Oriented). For some readers
It may have been used in AS (or other languages) for many years. To help everyone learn, I will briefly introduce
Let's take a look at these basic knowledge. Even the OOP experts of as 2 want to skip this section, because as 3.0
As long as you are in Flash
The working principle has indeed changed a lot.If you say you have never used a class, you are wrong,
After writing the code, the class is actually used. Class can be simply understood as an object, movieclip is a shadow
The video editing class. The text box, video editing, buttons, strings, and values all have their own classes.
The most basic two parts of a class: attributes (data or information), actions (actions or what it can do ). Attribute
(Property) is used to save information variables related to this class, Behavior refers to a function. If a function
Is a part of this class, so we call it method ).
A basic class:
We all know that we can create a component in the library, which can be used on the stage.
Creates many instances. Similar to components and instances, a class is a template, and an object (like an instance) is a class.
. The following is an example of a class:
Package {
Public class myclass {
Public var myproperty: Number = 100;
Public Function mymethod (){
Trace ("I Am Here ");
}
}
}
This code is described first. Here, we have some new knowledge, as well as for the AS 2 veteran: Package declaration. Package
The function is to group related classes. Knowing this is enough. We will not discuss it in depth,
(Package ),
The examples in this book do not even use packages. The keyword package and a pair of braces are mandatory.
Identify the package, followed by the definition of the class.
Another change is that the class in as3.0 has the access keyword. An access keyword is used to specify other code.
Whether the keyword of the code can be accessed. Public (public class) KEYWORDS indicate that the class can be accessed by code of any external class.
The classes in all examples in this book are public. After in-depth study of as 3.0, we will find that not all classes have
It is public, and there are even multiple classes, which are beyond the scope of this book.
In this example, we can see that the class name is myclass, followed by a pair of braces. There are two
One is the variable named myproperty, and the other is the function named mymethod.
Package)
Packages are mainly used for organization management. A package consists of the directory paths where the class is located and can be nested with multiple layers. Package name
It refers to a real folder, separated. For example, there is a class named utils
In the folder COM/friendsofed/makingthingsmove/(using the domain name as the package name is an unwritten
The purpose is to ensure that the package name is unique ). This class is written
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 package name and the class name writing class, for example:
Package com. friendsofed. makingthingsmove {
Public class utils {
}
}
Import)
Imagine that every time you want to use the method of this class, you must enter
Com. friendsofed. makingthingsmove. utils. Is it too cumbersome and too rigid. Don't worry, import
Statement can solve this problem. In this example, you can put the following sentence above the class definition in package:
Import com. friendsofed. makingthingsmove. utils ;.
Constructor)
A constructor is a method with the same name as a class name. This function is automatically called when the class is instantiated,
You can also input parameters, for example:
First, create a class:
Package {
Public class myclass {
Public Function myclass (Arg: string ){
Trace ("constructed ");
Trace ("You passed" + Arg );
}
}
}
Then, assume that you are working in the Flash CS3 IDE (integrated development environment) and create the instance on the timeline:
VaR myinstance: myclass = new myclass ("hello ");
Result output:
Constructed
You passed hello
Inheritance)
A class can inherit (inherit) and extended (extend) from another class. This means that it obtains another
All attributes and methods of a class (except those hidden by private ). The generated subclass (derived class) also
You can add more attributes and methods, or change existing attributes or methods of the parent class (base class. Create two classes to implement
Now (two independent. As files), for example:
Package {
Public class mybaseclass {
Public Function sayhello (): void {
Trace ("hello from mybaseclass ");
}
}
}
Package {
Public class mysubclass extends mybaseclass {
Public Function saygoodbye (): void {
Trace ("goodbye from mysubclass ");
}
}
}
Do not forget that each class must be in its own file. The file name is the class name and extension. As, so
The mybaseclass. As file and mysubclass. As file must be available. Therefore, when using Flash CS3 IDE,
The saved FLA file must be in the same folder as the two classes.
The following code will produce two instances and write them into the timeline to see what will happen:
VaR base: mybaseclass = new mybaseclass ();
Base. sayhello ();
VaR Sub: mysubclass = new mysubclass ();
Sub. sayhello ();
Sub. saygoodbye ();
There is nothing to say about the first instance. It is worth noting that the sayhello method in the second instance, although
Sayhello is not defined in mysubclass, But it inherits from mybaseclass class. Another noteworthy
A new method saygoodbye is added, which is not available to the parent class.
The following describes how to change an existing method in the parent class in the subclass. In as 2, we only need to re-
Define this method. In as 3, the override keyword must be clearly written to repeat
Definition.
Package {
Public class mysubclass extends mybaseclass {
Override public function sayhello (): void {
Trace ("Hola from mysubclass ");
}
Public Function saygoodbye (): void {
Trace ("goodbye from mysubclass ");
}
}
}
Note that the original sayhello method is overwritten, and new information is available after mysubclass is called. Another
Private Members cannot be overwritten because they can only be accessed by their own classes.
Movieclip/sprite subclass
We can write a class by ourselves and then let another class inherit it. In as 3, all code is not written in
On the timeline, they must be inherited from movieclip or Sprite at the beginning. The movieclip class is video cutting.
The ActionScript template for object attributes and methods. It includes attributes that we are familiar with, such as the X and Y coordinates of a video,
Scaling and so on, which does not change much in as 3.
As 3 also adds the sprite class, which is generally understood as a video clip not on the timeline. In many cases,
If you only use code to operate on objects and do not involve timelines and frames, you should use the sprite lightweight class. If
If a class inherits from movieclip or Sprite, it automatically owns all the attributes and methods of the class.
You can add special attributes and methods for this class.
For example, when a game designs a space ship object, we want it to have a graphic and be somewhere on the screen
Move, rotate, and add the enterframe listener for the animation, as well as the mouse and keyboard listener. These can be
Movieclip or sprite to complete, so it must inherit from them. At the same time, some attributes such as speed can be added.
(Speed ),Fuel, damage, takeoff, crash, and shoot)
Or selfdestruct. This class is probably like this:
Package {
Import flash. display. Sprite;
Public class spaceship extends sprite {
Private var speed: Number = 0;
Private var damage: Number = 0;
Private var fuel: Number = 1000;
Public Function takeoff (): void {
//...
}
Public Function crash (): void {
//...
}
Public Function shoot (): void {
//...
}
Public Function selfdestruct (): void {