ActionScript 3.0 Step By Step series (4): Coming from the cry before object-oriented development: "learn to write reusable code"

Source: Internet
Author: User
Enhance code reusability. From the creation of reusable code, reusable code is encapsulated by refactoring from existing code to make it a reusable code block with a single function. In general, this sentence is "encapsulation" or "abstraction ".

In actual programming and development, we can create a method to implement code reuse instead of copying the same code every time, encapsulate the code in the method and call it as needed. If each task is slightly changed, but you do not want to copy the code to modify it once, we can pass parameters to the method to make it suitable for different situations.

This article is still a basic article, focusing on the basic knowledge of actinscript development, and how to use an excellent IDE environment such as flex to create an actionscript program in combination with the Adobe Flex CS3 development environment, and encapsulate program code based on class files to make it reusable code.

1. Start from creating a project

This article is mainly to lay the foundation for object-oriented development. I believe everyone is very familiar with one or more development tools in vs2003/2005/2008, so familiarity with the development environment will certainly be beneficial to the development work, now, let's get familiar with the development environment of Adobe Flex cs3.


In the Flex Project Creation wizard, enter a project name and select the path of the Project Storage. "Application server type" will be described in subsequent sections. Click "Next" to create the project.

After the project is successfully created, there will be an mxml file under the src Project (this file is described in the previous article). view its source view and you can see:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml"
3 layout = "absolute">
4
5 </MX: Application>
6

OK. What you can see here is the development model structure for developing the ActionScript application in the Flex environment. An mxml corresponds to an application interface. Well, should I put some code into the development model? You can use the creationComplete attribute to specify an initialization method for the application:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml"
3 layout = "absolute" creationcomplete = "Init ()">
4 <mx: SCRIPT>
5 <! [CDATA [
6 internal function Init (): void {
7 trace ("application Initialization Method ");
8}
9]>
10 </MX: SCRIPT>
11 </MX: Application>

2. Use methods to create reusable code

Now there is such a small demand that we need to calculate the sum of two numbers. OK. This is simple. Drag two text input box components to enter the data and display the calculation result. Put a button to execute the calculation command.

As shown in the preceding figure, the click method is added to the button. The code for the click method Init () is as follows:

1 <mx: SCRIPT>
2 <! [CDATA [
3 internal function Init (): void {
4 // trace ("application Initialization Method ");
5 var a: int = int (txtA. text );
6 var B: int = int (txtB. text );
7 txtC. text = (a + B). toString ();
8}
9]>
10 </mx: Script>

Through the above attempts, we have learned how to encapsulate code using methods. How can we achieve reusability? I won't say much here. Like other languages, to achieve high flexibility reuse, We Need To refactor the existing code, for example, we can refactor the above Code to make it truly reusable. First, we need to figure out the functional requirements. We need a reusable method to calculate the sum of the two numbers. OK. Let's refactor it!

1 /*
2 * method function: Calculate the sum of two numbers
3 * parameter description:
4 * a: integer
5 * B: integer
6 * return value: return the sum of the two passed Integer Parameters (int)
7 */
8 private function Add (a: int, B: int): int {
9 return a + B;
10}

Through the reconstruction above, a general reusable method is abstracted. In the future, it will be OK to directly use this method for calculation and. As shown above, we have completed the encapsulation of program code through methods to achieve reusable purposes.

Note:

The above example is for ease of understanding. In actual programming development, it is not just such a simple encapsulation, And the refactoring process is not that simple.

 

3. develop a good habit of using class encapsulation methods

In actual development, the encapsulated program code is not as simple as we have demonstrated above, and needs to be encapsulated in a more concentrated, orderly or according to certain rules. Classes are usually used to encapsulate methods, while Methods encapsulate code that implements specific business functions. OK. When it comes to classes, we will start from creating a class file.

The figure is more convincing than the article, and the process of creating class files can be directly scanned. OK. continue to the next figure:

In the preceding two steps, an ActionScript class named book is created, and the corresponding file is book. As. The structure of the newly created book class is as follows:

1 package ActionScript. OOP
2 {
3 public class Book
4 {
5 Public Function book ()
6 {
7}
8}
9}

The Programming Method in ActionScript is similar to that in Java. Similar to Java, it has the concept of a package, just like the namespace in. net. The code above is fast, but the difference is that a keyword "function" is added to the method definition ". OK. We will add two methods for the Book class to get the Book name and price:

1 private var price: Number = 55.00;
2 Private var name: String = "ActionScript development manual ";
3
4 Public Function getbookname (): String
5 {
6 return this. Name;
7}
8
9 pulic function getbookprice (): Number
10 {
11 return this. price;
12}

Through the above step-by-step efforts, you have mastered the use of classes to encapsulate methods.

Note:

The above example is just for ease of understanding. In actual programming development, it is not just such a simple encapsulation, and should be handled according to the actual business logic.

 

3. Call reusable code methods

The process should start and end. With the above class, although it is only a semi-finished product used for demonstration, it is impossible for all the above efforts to get to the fore. Next we will call the encapsulated method.
The class to be encapsulated externally is the same in ActionScript and Java. it is OK to Import the external package to the current project through Import. As shown in:

The Book class we created before the import can call the public methods encapsulated in the class by instantiating the class object, which is the same as C #/Java. So far, using the Flex development environment and using the class encapsulation method to make it reusable code will be introduced here. More in-depth knowledge will be introduced in subsequent chapters.

Iii. Use exceptions to enhance code robustness

Exception in actionscrui 3.0, which is the same as C #/Java, is not described in detail here. If you are interested, refer to the following Connection article.
Exception and Exception Handling (C # programming guide): http://msdn.microsoft.com/zh-cn/library/ms173160.aspx. If you want to learn about and learn about the exception classes of ActionScript, refer to the documentation. For more information about the official online help documentation, see the second article in this series. The ActionScript 3.0 Step By Step series (II): build a solid foundation for the syntax of the ActionScript 3.0

Iv. Summary

1. Be familiar with the Adobe Flex development environment.

2. Use the action class file Encapsulation Method in flex to make it reusable code and call.

3. Exception mechanism in ActionScript 3.0.

 

 

 

 

 

From: http://www.cnblogs.com/beniao/archive/2008/12/24/1359233.html

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.