Archive

Posts Tagged ‘class’

AS3 – Object Oriented Programming – Part1

April 16th, 2009

We talked a lot about classes and objects in the introductory part, now let’s look how we can create a class.

In AS3, a class must be created in a separate .as file, with the name of the class being the name of the file.
Also, a class must be contained in a package. We’ll discuss in the next part more about packages.

To define a class in actionscript we have the keyword class.

To write our first class, named Apple, create a file named Apple.as, open it in a text editor and paste in the next code (make sure you edit with a text editor and not a word processor like MS Word):

package {
  public class Apple {
  }
}

There it our first class, however is quite empty; it has no functionality.
Notice that i’ve used the keyword package. That way I defined the package for the Apple class. But more on packages in the next part of this tutorial.
Also notice another keyword introduced: public. This is an access specifier.
An access specifier sets the visibility for a class, method, property or namespace.

A method is a function defined inside a class.
A property or member is a variable defined inside a class.
A namespace is like a package, controls the visibility of definitions, but it’s not directly related to classes, so it won’t be discussed in this tutorial.

In our case, the public specifier sets the class to be visible and accessible anywhere. Without specifying public, the class would be visible only inside the package, because the default access is internal.
A class can only be defined public or internal, however there are 2 other access specifiers: private and protected.
Private means that the property or method is only visible and accesible inside the class. About protected we’ll talk a bit later.

With this said, let’s add a method and a property to our class:

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
    }
  }
}

Now our Apple class has a property of type uint, named stones, which is declared private (this means that only methods of the class Apple can access this property) and has a default value of 4.
It also has a method, declared public, named Apple. Notice the name of this method is exactly the name of the class. A method with the same name as its class is named constructor.
In actionscript 3.0 a constructor can only be declared public. Also a constructor cannot have a return type (cannot return a value).
It is named “constructor” because when an object is created this method will be called. So it is a good place to put here any initializing code.

With these being said we can construct our first object of the class Apple, but first some words about the import statement.
Before we can use our class, we must import it where we want to use it. For that, we have the “import” statement, so we can use it like this:

import Apple;

Ok. Now we’re ready.
Open the Flash IDE, create a new Flash Actionscript 3.0 File, save it to a name of your choice and copy the Apple.as file in the same directory as your .fla file.
On the timeline, select the first keyframe and open the Actions window (Windows->Actions or press F9), and write the next lines:

import Apple;
var myApple : Apple = new Apple();

That’s it! Your first object is created out of your first class! You can test your file now, but really there isn’t much to see…
Notice the new keyword: it is used to construct new objects.

So let’s add something so that we can see what’s going on.
Add a trace action inside our constructor.
Our class should look like this:

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
      trace("A new apple is constructed!");
    }
  }
}

Now run again your movie and you should see “A new apple is constructed!” in the Output window. That’s because when we construct our object using the new statement, the constructor of our class is called, thus the trace method is called.
If you would construct more objects, you will see that the message is outputted once for every object.

Next, let’s say we would like to know how many stones has our apple object. But the stones property is private, which means we can’t access it from outside the class. So, to access it let’s add a new method, named “getStones”, that will return the number of stones, and make it public.

package {
  public class Apple {
    private var stones:uint = 4;

    public function Apple() {
      trace("A new apple is constructed!");
    }

    public function getStones() : uint {
      return this.stones;
    }
  }
}

Make note of this keyword. “this” refers to the “current object”.

Now we can call the new method on our object like this:

myApple.getStones()

In Flash, change the code with this one:

import Apple;
var myApple : Apple = new Apple();
trace("myApple has "+myApple.getStones()+" stones.");

When you run your movie you should see in the output 2 lines.

That’s about all on this first part of the tutorial.
In the next part we’ll discuss more about packages.

Actionscript 3.0, Object Oriented Programming , , , , , , ,

AS3 – Object Oriented Programming – Intro

April 16th, 2009

Programmers who are not-familiar with Object Oriented Programming, find it hard to understand and cope with the concepts of object oriented programming. And moving from function oriented programming to object oriented programming can be a real pain.
That’s because they don’t have the right thinking. They think, “hmm, so I have this instance of this class and i must call this function on this object; for that I must use the . (dot) operator…”.
Actually OOP tries to resemble nature. That’s right, nature proved us it’s way for millions of years, why not take after it?… or something like this :-)

Let’s look at a nature example:
We have this big, beautiful apple. We look at it and we think “all apples look roughly the same”. So we take our apple, we look even closer to it and realize that even though it looks like all the other apples, there’s something particular about it: it’s a “Golden Delicious” apple. Now we know 2 things about it: it’s an apple, and furthermore it’s a Golden Delicious apple. We realize now that we can separate all the “Golden Delicious” apples from other classes of apples.
So here we are now:
We have the Apples class, which contains the Golden Delicious class and all the other apple classes: like Ultra Red Gala, Cameo, etc.
Furthermore, we can say: “but all the apples are fruits!”. Which is true.
This creates the next hierarchy:

  • Fruit
    • Apple
      • Golden Delicious
      • Ultra Red Gala
      • Cameo

Ok we know something about classes, but what is our apple? It’s not a class, it can’t be a class because it’s an object. That’s right: the class of our apple is “Golden Delicious”, the base class is “Apple”, but our apple is an instance of the class “Golden Delicious”.

Ok, that’s a very simplistic way of looking at Object Oriented programming.
This is only an introduction, as OOP is very complex, OOP in AS3 is complex too, so i’ll try to explain everything that’s needed to find your way into OO programming world.

I think it’s worth mentioning here that OOP concepts are common on all OO programming languages, however the syntax and features may differ from language to language.

Follow on to the first actual part of this tutorial: AS3 – Object Oriented Programming – Part1

Object Oriented Programming , , , ,