Hello, today we will cover the basics of Object Oriented Programming, I'm basing this off a java lecture, but it's fully converted to flash
Here we will cover:
Objects (instances)
Classes
Object References
Methods (also covered in AS: Functions )
Method Signatures (will only be available effectivly in flash 8)
An example
inheritence, interfaces, abstracts and so on in the next tutorial
Objects are everything, we use objects and we create out own types of objects (classes)
Running the program creates objects from the classes (for example _root.a=new Sound() creates a new sound object).
Every object has properties a constructor (sometimes auto-triggered) and methods.
for example, let's look at MovieClips, MovieClip is a class eventhough we probebly never thought about it, it's an object type, it has methods like
_x
_y
_alpha
_depth //which we can't access becuase macromedia designed it that way
and so on
it has methods like
setProperty (which we don't use anymore :P)
duplicateMovieClip
startDrag
getDepth
swapDepths
and so on
and it has a constructor which we don't use alot
now another example is Arrays, they have attributes like length and their content and they have methods like push() pop() and 3 constructors Array(); Array(object1,object2...objectN) and Array(length), whenever you use 'new' you trigger the constructor.
the world consists of objects:
some are tangible – A MovieClip, a button, a component.
some are intangible – A string, an array, a sound, (and most of your own classes)
a Flash program, when running, populates the "world" with objects (like in your game player, inventory, map, and so on), and specifies how they will interact.
this (partial) correspondence between real-world objects and software objects is the first step towards reducing the conceptual gap in software development. OOP allows us to make very readable code.
EXAMPLE:
a new MyCar Object, it needs the engineSize numDoors and colour attributes, and the drive() reverse() and lock() methods, the drive method uses engineSize, the lock method uses numDoors, within the object every function has access to the object's attributes.
In the real world, an object is a thing which has attributes and which behaves in particular ways. In Flash, an object is a collection of data and methods. The object encapsulates the data and methods. In a software model, the object provides an abstraction of the real-world entity
What is a class?
class is simply a specification of an object. This allows us to create multiple objects all of the same type.
For example, we might say there is a class "Car", describing cars in general. It has attributes (engine size, number of doors, colour, etc.) and behaviours (drive, reverse, open door, etc.).
We can then talk about particular objects in this class - for example, "Ken's car", "John's car" - they all have the same attributes and behaviour, but are different objects.
for example, we can have
Kens_car=new MyCar();
Inglor_car=new Mycar();
and inglor's car and ken's car get all the objects and properties of "MyCar"
to refer to an object, we use a name or referring phrase
a reference is a phrase used to refer to an object
"CS2200"
"my car"
"Ken Brown"
"CS2200 lecturer"
most objects we create in flash will have at least one reference, and many of them will have multiple references
To create an object,
we must specify what class it will be an instance of
we should give it a reference, so that we can refer to it
Example: to create an object from the MovieClip class, type:
var mc:MovieClip = new MovieClip();
The methods of an object are the pieces of behaviour it can be asked to carry out (or decides to carry out itself):
examples:
the tasks a person will perform
-delivering a lecture, completing an assignment, pouring a pint, ...
the functions of a machine
-accelerate, wash clothes on cycle 5, play track 3, ...
the facilities of an abstract object
-display your bank balance, increase your balance when you deposit money,
Provoking behaviour
What makes an object carry out one of its methods?
requested by another object (if it has access to it)
I ask my car to start
salesman asks finance officer to do a credit check
requested by the object itself
I decide to brush my teeth in the morning
car switches on fan when overheating
triggered by an event
door slams: I flinch
by passing a message
To pass a message, or provoke behaviour, you need to know
the name or location of the object
the command or action that provokes the behaviour
You can also directly invoke a method as following:
E.g. Barry says
"Moira, please do this credit check for me"
code example:
_root.object.object2.doMethod();
we usually specify some information when calling a method, passed (ususally) in params when calling an object not in the class we call it from, for example
_root.object.object2.traceFrom("Hello World");
methods may also have return values (explained in the functions tutorial)
Method signatures
if object A is to invoke one of object B's methods, it must know the method signature:
method name
the parameters, in the correct order and with the correct data type
the data type of the result
in flash it's not as critical, but since ver 8, it's a biggie.
Finally Syntax
classes only work in external .as files, with mx 2004 pro create a new .as file, in mx 2004 create a new .as file in notepad.
declare the class
class ClassName{
to declare a class you use the saved word "class" followd by the class name, remmember the file (the .as file) must have the EXACT SAME NAME;
accesstype var varname:VarType;
the accesstype in flash for now is either private (meaning only the class can access this var, or public (meaning anyone can)
following this will be the construction
function ClassName(params){//remmember, a constructor has NO RETURN TYPE
this.varname=params;
}
following the constructor are all the methods
function methodOne():Void{
this.varname++;
}
remmember, if you want your class to be able to trace, I'll post some examples soon