|
-
OOP is one letter from OOPS
Dissecting a Class
So in the last post we looked at some of the very basic things about AS classes. In this post I am going to go a bit further and take a look at an example of basic class.
So in the last post we examined basic class structure such as (examples in this post are AS 2.0 syntax)
class SomeClass {
function SomeClass(){
}
}
This is as simple as a class can get and this class would not actually do anything. So lets take a look at a class that actually does something (all be it something useless and simple)
The following would go in an external actionscript file named Person.as. For simplicity sake it would be saved in the same folder as the FLA (setting up class paths will be in another post. For a primer on setting up class paths see http://www.flash-mx.com/flash/actionscript_lott2.cfm. Look here is nothing happens when you test the following. I had to tweek the class path in flash 8 to get this to run).
class Person {
// example instance variable
var _myName:String;
//constructor function
function Person(myName:String) {
_myName = myName;
trace("New Person");
}
//example method
function sayHello() {
trace("Hello, my name is "+_myName);
}
}
Then in Flash use the following code on an actions layer in the first frame:
var person = new Person("Bill");
person.sayHello();
When you test the movie, you should see the following in the output window:
New Person
Hello, my name is Bill
Here we saw the use of the keyword new followed by the name of the function and in this example we passed a string as the parameter (myName:String). The constructor (the function with the same name as the class and the AS file) is automatically called and executes any code in its body. In this case, it is setting an instance variable _myName (a standard coding convention is to place an underscore in front of instance variables to avoid naming conflicts).
The next line:
person.sayHello();
calls the sayHello() function of the person instance which causes the following function in the class file to execute:
function sayHello() {
trace("Hello, my name is "+_myName);
}
so we have done several things. First we created an instance of the Person Object (class) and in doing so we set the instance variable _myName to the value passed to the constructor function. Then we called the sayHello method.
Now that we have this basic class set up, we can create multiple instances. For example:
var bill = new Person("Bill");
bill.sayHello();
var jane = new Person("Jane");
jane.sayHello();
var sam = new Person("Sam");
sam.sayHello();
Would output:
New Person
Hello, my name is Bill
New Person
Hello, my name is Jane
New Person
Hello, my name is Sam
So here we can see that with the same class (object) we can assign different properties and call methods on several instance of the same class which all originate from the same AS file (object/class).
That's all for now.
-
OOP is one letter from OOPS
OOP Basics
Well now that I have posted some basic examples, I am starting to realize that I have been leaving out some vary basic concepts thus far for the sake of simplicity. So let's take a moment and back track (this should have probably been the first post, but hey - it is a work in progress).
Most of the following can be found in "Object-Oriented Programming for Flash 8" by Peter Elst and Todd Yard with Sas Jacobs and William Drol published by firendsof ED. (No I don't get royalties I just think the original author's should get credit for their work)
First thing. I was a bit to causal about using class and object interchangeably.
Classes and Objects
According to the previously mentioned book, the relation ship between a class and an object is like that of the relation ship between a a blue print and a house. The blue print is not the house itself (nor can you do anything with it, other than use it to build a house), it is only the specification for what the house needs to provide in terms of structure and function. A house would be the object. It is the manifestation of the specification. You could also build several houses from the same blue print. So, the houses could be thought of as instances of the blue print. Hence in OOP objects are often referred to as class instances.
OK so that was the first thing I was a bit unclear on. There were other things I just left out(such as data typing, but that shall come later, for now more basics).
Properties
Properties allows objects to have unique qualities. In the example in the last post we assigned a unique name to several instances. Properties are generally held in instance variables.
class Person {
//this is an instance variable and hence a property;
private var _myName:String;
function Person (n:String){
_myName = n;
}
}
In Flash:
//same class two instances with _myName property set to two distinct values
var personOne = new Person("Bill");
var personTwo = new Person("Jill");
Encapsulation
Encapsulation is about hiding the inner workings of how classes accomplish things. So good encapsulation is worthy of its own post, but as a basic idea,
a person who will end up using a class should not have to know anything about what the code inside the class looks like. All they should have to know is what the public (more on that later to, for now just think of it as the methods the user can call) methods are, what data they have to pass to them, and what data or behavior they should expect to get when calling the method. How the method does what it does is of no concern to the end user. How the methods work is the concern of the person creating the class and if they design it well then the end user should not have to know anything about the internal workings of the class to be able to use it. (least thats the theory)
and finally for this post ...
Inheritance
Ok I will be honest, this topic could (and does) span chapters or books. Boiling this down to a paragraph is going to be messy. But here we go.
Simply put inheritances allows us to reuse code from other classes in new classes. When one class inherits from another (with some qualifications, this works differently in different languages), the new class inherits (can use or has) all of the methods and properties of the class it inherits from. So lets say we have:
class Ball{
//this is a property
_size:Number
function Ball(s:Number){
_size = s
}
//most balls bounce
function bounce(){
//code that makes the ball bounce
}
}
class BasketBall extends Ball{
function BasketBall(s:Number){
super(s);
}
}
in Flash:
var bb = new BasketBall(5);
bb.bounce();
OK the short version of what we just did. Class basket ball inherits from class ball (that is what the extends keyword does). This mean that we can call the bounce function that is defined class ball since basketball inherits from ball. We did not have to redefine bounce (unless we want to, but thats overiding and thats another post again) in any class that inherits from ball.
OK so, again I am still being very basic and leaving some things out for simplicity sake, but we are moving along.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|