chapter iii(oop)

22
Object-Oriented Programming

Upload: chhom-karath

Post on 18-Jan-2017

18 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Chapter iii(oop)

Object-Oriented Programming

Page 2: Chapter iii(oop)

Introduction• large projects, programmers use both OOP and design patterns to deal with

change.• (OOP) is a specific programming technique to create chunks of programming code

(called "objects") that do a specific job, and divide the script into distinct pieces that can be easily managed.

• OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects.

• Objects are defined using two primary identifiers (or members): – Properties - are characteristics that define an object.

- For example, "color" can be property of a DVD player.• object.property.method();:(square.graphics.beginFill(0x08fe08); )• object.property = value;:(square.width = 100; )

– Methods - are actions that can be performed by an object.For example, Play, and Pause are methods of a DVD player.• method(parameter1, parameter2):(myClip.gotoAndStop(9); )

Page 3: Chapter iii(oop)

• In addition to objects, properties and methods, in OOP there are elements known as "events".event - is the action of an application or user, such as a mouse click, that triggers an action related to an object.So, the following elements are used in OOP (Object Oriented Programming):classes - is a collection of related properties, methods, and events that are grouped in one colection. It contains the code which define the data type, state, and behaviors of an object.- A class is like a generalized blueprint for building an object.•Instances - is a specific object that can uses the properties and methods of a class.•Properties - characteristics that define an object.•Methods - actions that an object can perform.•Events - actions that can trigger the execution of a function, or instructions.Events can be the actions of the mouse cursor (mouse clicks or mouse movements.), the keybord actions (pressed keys).There are also Frame events (like the Flash playhead moving into or out of specific frames), Load events (which report on the progress when loading external files), and other event types.

Page 4: Chapter iii(oop)

• OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects.

• A class is a "blueprint" for an object, is a code template used to generate objects. It contins the instructions that define the properties and methods that an object can use.

• Objects are elements from the script that are defined to perform the instructions written in a class, and can use the properties and methods defined in the class.– For example, to understand, you can think of a class as a blueprint for constructing an house. Many

houses can be built from the same blueprint, so, the object is the house created according to that blueprint. You can build multiple objects that have the properties defined in the class.

• Defining a classpublic class ClassName { level access var property1; level access var property2; ................. level access function method1() { // Funtion code } level access function method2() { // Function Code } .................}

level of access•public - available in all script•private - available only inside the same class•protected - available in that class and its sub-classes•internal - available in the classes of the same package-If no attribute is added, it is considered "private“

Page 5: Chapter iii(oop)

TestClas.as(class name and file the same)// create the packagepackage { // define the class, with public attribute, a property "prop" and a method "metod" public class TestClas { public var prop:Number = 7; // Public property protected const HIDEN:int = 2; // "Protected" constant

// Creating the method, takes an argument (Number type) public function metod(val:Number):void{

this.prop = val + HIDEN; }}}TestClas.fls

// Creates the object instance of the TestClas classvar tst:TestClas = new TestClas();// Checks the value of the "prop" property trace(tst.prop); // 7// access the "metod()" method, which change the value of "prop"tst.metod(18);// checks "prop"trace(tst.prop); // 20

Page 6: Chapter iii(oop)

The Accessor methodpackage {public class testClas2 { // Defining properties without value public var prop1:Number; public var prop2:String;

// Create accessor method public function setProp(prop1:Number, prop2:String) { // Assign values to the properties (the ones with "this") this.prop1 = prop1; this.prop2 = prop2; }}}// Create object instance of the testClas2 classvar obj4:testClas2 = new testClas2();// Verify in Output 'prop1' and 'prop2'trace(obj4.prop1+"-"+obj4.prop2); // NaN-null// Call the accessor methodobj4.setProp(8, 'Tutorials');

// Verify 'prop1' and 'prop2' againtrace(obj4.prop1+"-"+obj4.prop2); // 8-Tutorials

- The "this" instruction specifies exactly which the property is, and makes the difference between property and parameter with the same name

Page 7: Chapter iii(oop)

Static properties• Specifies that a variable, constant, or method belongs to the class, rather than to

instances of the class.

No overloading Method in actionScript 3.0

Page 8: Chapter iii(oop)

The constructor• The constructor is a special function inside the class. It must have the same name

as the class. This method is always "public" even if this attribute is not mentioned.• The constructor is automatically called when an instance of the respective class is

created.• Another difference from other functions is that the Constructor not use the

"return" instruction.

Page 9: Chapter iii(oop)

Package• The Package is a structure through which multiple classes can be grouped together. This helps to create

different classes that can have the same name. • multiple classes can not be created in the same "package" body. This actually represents a folder where

the "AS" files with classes that belong to the same group are added. In the "package body" (between it's curly braces) only one class can be created.

• - "packageName" is optional, for beginners it's better to not specify it, because needs extra things (explained below).packageName Folder– package packageName {

attribute class ClassName { attribute var property1; attribute var property2; ................. attribute function method1() {

// Function code } attribute function method2() {

// Function code} ................. }}

class1.flv– import packageName.ClassName;OR:import packageName.*

Page 10: Chapter iii(oop)

Group FolderPerson.as

package group{public class Person{

public var personal:String;public var age:int;}

}FullName.as

package group{public class FullName{private var firstName;private var lastName;public function setFirstName(fName:String){

firstName=fName;}public function setLastName(lName:String){

lastName=lName;}public function getFirstName(){

return firstName;}public function getLastName(){

return lastName;}

}}

Flash.flvimport group.*;

var full1:group.FullName=new group.FullName();full1.setFirstName("Chan");full1.setLastName("Dara");trace(full1.getFirstName() + " " + full1.getLastName());

var p1:group.Person=new group.Person();p1.personal="Cambodia";p1.age=10;trace(p1.personal + " and age: " + p1.age);

Page 11: Chapter iii(oop)

TestClas.aspackage { // define the class, with public attribute public class TestClas { public var prop:Number = 7; // Public property protected const HIDEN:int = 2; // "protected" constant

// Creating the constructor public function TestClas(nr1:Number, nr2:Number) { // stores in a variable the arithmetic average of the "nr1" and "nr2" parameters var average_a = (nr1+nr2)/2; trace(average_a); // Returns in output the "average_a" value }

// Create method (takes a Number type argument) public function metod(val:Number):void {

this.prop = val + HIDEN; }}}Otherfile.flvvar tst:TestClas = new TestClas(7, 8);

Page 12: Chapter iii(oop)

Classes - Static Elements• In a class in ActionScript you can also create static elements (variable, methods,

constante). • Static elements belong exclusively to a class, and can not be accessed through an

instance of the class, but directly using the name of the class.Static properties• attribute static var prop_name:DataType = value;

– Static properties belong to the class and can be accessed with this syntax:

• ClassName.prop_name Static Methods• To create a static method, add the keyword static before the keyword function, in the

method definition.attribute static function methodName() { // code to execute}

• To call a static method, use this syntax:ClassName.methodName(parameters)

• In the body of a static method you can only use other static elements (properties, constants, methods). The keyword "this" can not be used

Page 13: Chapter iii(oop)

elStatic.aspackage { // Define "elStatic" class public class elStatic { // Define properties public var prop1:String = 'www.coursesweb.net'; public static var prop2:String = 'ActionScript'; // Static variable }}

Anyfile.flv// Create instance of the class "elStatic"var obj2:elStatic = new elStatic();

// access the class constantstrace(obj2.TUTORIALS); // web development// trace(obj2.SITE); // Returns an error

// access 'SITE' using the class nametrace(elStatic.SITE); // coursesweb

Page 14: Chapter iii(oop)

package {public class elStatic{ public var prop1:String = 'www.coursesweb.net'; public static var prop2:String = 'ActionScript'; // Static variable public const TUTORIALS:String = 'web'; public static const SITE:String = 'coursesweb'; // Static constant // Defining a static method public static function Courses(param:String):String{ var re:String; // The variable that will be returned switch(param){ case 'web': re = elStatic.prop2; break; case 'coursesweb':re = SITE; break; default: re = 'Default value';} return re; // Returns "re" value}}}

var obj3:elStatic = new elStatic();trace(elStatic.Courses(obj3.TUTORIALS)); // ActionScript// trace(obj3.Courses(elStatic.SITE)); // generate an error

// Call the static method, uses a static constant for argumenttrace(elStatic.Courses(elStatic.SITE)); // coursesweb

Page 15: Chapter iii(oop)

Inheritance - Parent class and Child class• Inheritage is a characteristic of the classes that lets you to transfer /use properties,

constants and methods from one class to another, in an hierarchical structure. • A child class inherits all public and protected properties and methods from the

parent, and can use them in it's own code and transmits them when an instance of the child subclass is created. As in nature, children inherit the parent's genes.

• A class that inherits from another is said to be a subclass of it, or child class.A child class inherits all public and protected properties and methods from the parent, and can use them in it's own code and transmits them when an instance of the child subclass is created. As in nature, children inherit the parent's genes.

Page 16: Chapter iii(oop)

Mammal.aspackage{public class Mammal {private var _sName:String;public function getName():String {

return _sName;}public function setName(sName:String):void {

_sName = sName;}}}Rabbit.aspackage{public class Rabbit extends Mammal {private var _sColoration:String;public function Rabbit(sName:String, sColoration:String)

{setName(sName);this._sColoration = sColoration;}

public function getColoration():String {return _sColoration;

}}}

Name.flvvar ob1:Rabbit = new Rabbit("Champa", "White and Brown");trace(ob1.getName() + ' has color: ' + ob1.getColoration() );

Page 17: Chapter iii(oop)

Mammal.aspackage{public class Mammal {private var _sName:String;protected function getName():String {

return _sName;}protected function setName(sName:String):void {

_sName = sName;}}}Rabbit.aspackage{public class Rabbit extends Mammal {private var _sColoration:String;public function Rabbit(sName:String, sColoration:String) {

setName(sName);this._sColoration = sColoration;}

public function getColoration():String {return _sColoration;}

public function getNameRabbit(){return getName();}

public function setNameRabbit(sName:String){setName(sName);}}}

Name.flvvar ob1:Rabbit = new Rabbit("Champa", "White and Brown");trace(ob1.getNameRabbit() + ' has color: ' + ob1.getColoration() );

Page 18: Chapter iii(oop)

• To rewrite a method use override keyword in the definition of that method in the child class. With this syntax:

override attribute function methodName() {

// The code that will replace the original one }

• Rewritting a method does not affect the original in the parent class, the modifications are available only in the child class, respectively in its subclasses.– To keep using the initial code (from the parent class) in the rewritten function,

include it with super.methodName(); in the body of the function in child class.– "super.methodName()" includes "methodName()" from the parent class.

Page 19: Chapter iii(oop)

Mammal.aspackage{public class Mammal {private var _sName:String;public function getName():String {

return _sName;}public function setName(sName:String):void {

_sName = sName;}}}Rabbit.aspackage{public class Rabbit extends Mammal {private var _sColoration:String;public function Rabbit(sName:String, sColoration:String) {

super.setName(sName);this._sColoration = sColoration;}

public function getColoration():String {return _sColoration;}

override public function getName():String {

return super.getName();}override public function setName(sName:String):void { super.setName(sName);

}}}

Name.flvvar ob1:Rabbit = new Rabbit("Champa", "White and Brown");trace(ob1.getName() + ' has color: ' + ob1.getColoration() );

Page 20: Chapter iii(oop)

package{public final class Mammal {private var _sName:String;public function getName():String {

return _sName;}public function setName(sName:String):void {

_sName = sName;}}}

Page 21: Chapter iii(oop)

Classes - Interface in ActionScript 3• Interface is a class used as a pattern, or template for classes with similar functions,

that must respect a certain basic structure. • An interface is a collection of method declarations that allows unrelated objects to

communicate with one another • the Interface is a class with a list of required methods that must be created in the

classes where it is implemented. • All the methods specified in the "Interface" must be defined in the classes where

it's applied, having the same name, data type and parameters as indicated in the "Interface". – package {

public interface InterfaceName {function methodName(prop1:DataType, prop2:DataType, ...):DataType;function otherMethod(prop1:DataType, prop2:DataType, ...):DataType;

........... }

}– public class ClassName implements InterfaceName {

// Instructions}

Page 22: Chapter iii(oop)

ITest.aspackage { public interface ITest { function Links():void; function Tutorials(gen:String, nota:int):String}}webDevelopment.aspackage { public class webDevelopment implements ITest { protected var site:String = 'www.coursesweb.net'; public function Links():void{ trace(this.site); } public function Tutorials(gen:String, note:int):String { var re:String = gen+'-'+note; return re; } public function Diverse(val:*):void { trace(val); }}}

Name.flvvar obj:webDevelopment = new webDevelopment();obj.Links(); trace(obj.Tutorials('Flash', 8)); obj.Diverse(2010);