Beginning ActionScript 3.0 – Introduction to OOP

wallet-64x64 When you looking at some resources introducing the ActionScript 3.0, it may say that, “ActionScript 3.0 is an object-oriented programming (OOP) language”. The key word “object-oriented programming (OOP)” is the topic of this article eh, and the following articlesJ.

First sight on the OOP-style

When we talked about the control flow, we use an example to mimic my schedule. And the code is like this:

var newEmail:Boolean = false;
var articleFinished:Boolean = false;

trace(”have breakfast”); //sequence
trace(”practice calligraphy”);
trace(”check the email”);

if(newEmail) { //selection
trace(”Reply the email”);
} else {
trace(”Read blogs”);
}

while(!articleFinished) { //repetition
trace(”Busy writing”);
articleFinished = true;
}

Now, let’s change the code into OOP-style.

var authorInstance:Author = new Author();

authorInstance.haveBreakfast();
authorInstance.practiceCalligraphy();
authorInstance.checkEmail();
authorInstance.writeArticle();

Note: The OOP-style code is just to give you a first sight of the code in OOP style. It doesn’t work now.

So, what’s you first impression about the OOP-style code.

The code seems much shorter? Eh, if you try to get the code work, you will find that the code is longer than the previous one.

In my opinion, the first one is much more like organizing our thinking in the computer’s way; while the second one is much more like our human way of thinking. So, you may think the second one is much easier for you to understand, because, it’s just the way you do and think in your daily life.

So, the difference between structural programming and the object-oriented programming is the thinking way.

Object-Oriented programming is a complex concept, so I’ll just give you the basic idea here, you can find more resources by Google.

Now, let’s go back to the code.

var authorInstance:Author = new Author();

According to the previous article, you should know this statement can be dividing into two parts.

var authorInstance:Author;
authorInstance = new Author();

The first part is declaring an Author type variable, authorInstance. According to our previous experience, there isn’t having any data type called Author in ActionScript 3.0. Where is the data type, Author, from? In fact, we don’t treat the Author as a data type; we treat it as a class.

Before we explain the concept of class, we need to know one basic concept in OOP, object. As you know, OOP is short for Object-Oriented Programming. This programming paradigm is centered on object from the denotation.

For short, object is a basic element that contains data and operations. And class is the abstract definition of object. In our example, we treat the author as an object, and we use a class Author to define it. Furthermore, the authorInstance here is called object instance, and you can just call it object.

When we doing object-oriented programming, we often do the object-oriented analysis and design (OOA&D) first. We trying to find out the objects we’ll use in the program, then we can use class to describe them.

And the second part is allocating an Author object and assigns it to authorInstance. Remember, the new operator calls the constructor. Eh, the concept of constructor will be discussed later.

Now, let’s go on to the following code.

authorInstance.haveBreakfast();
authorInstance.practiceCalligraphy();
authorInstance.checkEmail();
authorInstance.writeArticle();

Separate the statements into three parts, the first part is the object instance name, the second part is the dot operator, and the third part is just like a function calling, actually, we call them methods.

The first part you’ve already know, it’s a instance name, points to an Author type object.

The second part is the dot operator (.). The dot operator in ActionScript (and many other programming languages) is used to indicate that a name refers to a child element of an object (such as a property or method). For instance, in the expression, instance.property, the dot operator indicates that the term property is referring to some value that is an element of the object named instance. And the expression, authorInstance.haveBreakfast(), means a calling to the method named haveBreakfast in the object authorInstance.

Now, you should know the meaning of each statement in the example with OOP-style code.

Maybe too much concepts here, hope you won’t get confused. :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

4 Responses to “Beginning ActionScript 3.0 – Introduction to OOP”

  1. [...] Read the original post: Beginning ActionScript 3.0 – Introduction to OOP [...]

  2. [...] When you looking at some resources introducing the ActionScript 3.0, it may say that, “ActionScript 3.0 is an object-oriented programming (OOP) language”. The key word “object-oriented programming (OOP)” is the topic of this article eh, and the following articlesJ. Read the rest here: Beginning ActionScript 3.0 – Introduction to OOP [...]

  3. [...] Learn how to keep players from leaving your game upon game over with a few simple additions. 4.) Introduction to OOP: A nice introduction to explaining the basics of Object Oriented Programming. 5.) Pre-loaders with [...]

  4. [...] we introduce the basic OOP concepts, we use an example to illustrate the OOP-style code(@see Beginning ActionScript 3.0 – Introduction to OOP). Inside the example, we use the new operator. Did you still remember the function of “new” [...]

Leave a Reply