Object Oriented Programming
Object Oriented Programming
com
312
Target Audience
Those familiar with ActionScript (AS) 2 (procedural programming or using the timeline) Those new to object-oriented programming and AS3
Those new to AS programming, but with experience in other non-OO programming languages
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 1
Topics
4
Concepts of object-oriented programming Procedural versus object-oriented paradigms Understanding the terms Working with classes Encapsulation Understanding inheritance and composition Coding with polymorphism
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 2
The concepts
ActionScript
AS 3 is object-oriented capable
Implementation of OO is optional
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 3
Course: TrainingShell.swf
. . .
Object-Oriented Terminology
Classes
A template, blueprint Involves hiding the details of the class Aids in the reuse of code An entity that exists in multiple forms; an entity having different behaviors Provides flexibility
Encapsulation
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 4
What is a Class?
Download copy
Download copy
10
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 5
Class properties
Instance name
11
// Instantiate the object var myObject:Button = new Button(); myObject.label = "Hello world"; // Add to stage addChild(myObject);
12
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 6
Directory: MindCrossExamples/Example1
Property: TotalWords
public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }
Flash file: Example1.fla
Method: WriteTotal
13
package Classes { public class Glossary { public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }
Directory
MindCrossExamples Example1a
14
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 7
Package { public class Glossary { public var TotalWords:int = 10; public function WriteTotal() { trace("Total words: "+ TotalWords); } } }
MindCrossExamples Classes
//no import statement required var myGlossary:Glossary = new Glossary(); myGlossary.TotalWords = 10; myGlossary.WriteTotal();
Directory
MindCrossExamples Example1a
15
Encapsulation
Primary benefits
Modularity Information hiding What methods and properties will be made public
Object design
16
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 8
Encapsulation
17
Inheritance
Extends the functionality A subclass includes all the functionality of a higher level class Everything from a higher level class is automatically applicable to lower levels in the hierarchy Classes can be created from other classes
Class hierarchies
18
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 9
Inheritance in Flash
19
Inheritance in Flash
MovieClip class
... ... Public class MovieClip extends Sprite ... 20 Public class MyClass extends MovieClip ...
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 10
Composition
Sometimes referred to as aggregation An object composed of other objects Contains an instance of a class
Composition Has a . . . Vehicle has a Tire Inheritance Is a . . . RaceCar is a Vehicle
21
Composition
Composition
... import tire; Public class Vehicle{ { ... var myObject:tire = new tire(); ... }
Inheritance
... Public class RaceCar extends Vehicle ...
22
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 11
Directory: MindCrossExamples/Example3
23
package { import flash.display.MovieClip; import flash.text.TextField; public class WelcomeExample extends MovieClip { private var greetingMsg:String; public function sayHello():String { greetingMsg = "User: Hello world"; // get user information return greetingMsg; } } } this.Greeting.text = myObject.sayHello();
24
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 12
package { public class Example4 { private var PrivTotalPages:int; public function get TotalPages():int { return PrivTotalPages } public function set TotalPages(setValue:int):void { Flash file: PropertyExample.fla PrivTotalPages = setValue; ... } var myObject:Example4 = new Example4(); } myObject.TotalPages = 53; } PageNumber.text = "Page "+currentPage+ " of "+myObject.TotalPages;
25
Polymorphism
x and y properties are referenced the same way regardless of which class they are part of
26
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 13
Polymorphism
Circle sub-class
Square sub-class
Area method
Area method
27
Constructors
Methods used when an instance of a class is created Required to have the same name as the class
Constructor method
... Constructors are automatically invoked during instantiation // Output - initiation code var myObject:Example6 = new Example6(); ...
28
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 14
Summary
OOP is a programming style Each class/object is responsible for a specific task A class is a combination of data and operations Encapsulation allows usage of the object, but, not knowledge of its coding techniques and private code An object is an instance of a class The objects functionality is dictated by the class from which it was created Classes are organized into a tree structure called inheritance hierarchy An object uses methods to provide functionality An object uses properties to hold variable information
29
Session 312 Introduction to Object Oriented Programming with ActionScript 3.0 Scott Hutton, MindCross Training
Page 15