1 / 34

Object Oriented Programming

Object Oriented Programming. Multimedia Studio. B.Sc. Digital Media. Agenda. Procedural Programming v OOP Software Objects Classes and Methods The Object Oriented Paradigm. Object Oriented Programming. Object-Oriented Programming is a way of describing

duaa
Télécharger la présentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Multimedia Studio B.Sc. Digital Media

  2. Agenda • Procedural Programming v OOP • Software Objects • Classes and Methods • The Object Oriented Paradigm

  3. Object Oriented Programming • Object-Oriented Programming is a way of describing and structuring software systems that features objects that contain functionality and data • Early program structure techniques kept program functionality and data separate - so called “procedural programming”

  4. Procedural Programming 10 readln(sum) 20 sum = sum + x 30 writeln(“The answer is” ), sum) . 60 GOSUB 200 . 200 readln(answer) . 300 return

  5. Object Oriented Programming • OOP places the data and functionality inside a software object - the data is said to be encapsulated within the object Software object data and functionality

  6. Object Oriented Programming • Software objects communicate with other software objects to carry out the overall function of a software system by sending messages to each other Software object Software object data and functionality data and functionality

  7. Transport System To model vehicles and drivers as an OOP software system think about the various objects, properties and functions of such a system: Vehicles - Type, Make, Model, Value, Registration Number Drivers - Name, Age, Date of Birth, License Type

  8. Transport System: Vehicle Object: Vehicles Properties (attributes) Type Make Model Value Registration Number Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehilce Set the value of the vehicle

  9. Transport System: Driver Object: Driver Properties (attributes) Name Date of Birth License Type Value Functions (methods) Create an object of type Driver Set the Name of a driver Get the name of a driver Set the type of licence Get the type of licence

  10. Vehicle Object Functions (methods) to communicate with a Vehicle object Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Get Registration No.

  11. Driver Object Functions (methods) to communicate with a Driver object Driver Properties: Name Date of Birth License Type Set Name Get Name Set Date of Birth Get Date of Birth Set Licence Type Get License Type

  12. Vehicle Object Public Visibility (Interface to Object) Vehicle Properties: Make Model Registration No. Value Set Make Get Make Set Registration No. Various Behavior Various Behavior

  13. Encapsulation (Data Hiding) Known and consistent public interface Vehicle Internal workings of class hidden users of the class access functionality via the public interface Owners of the class may change internal representations inside the class but maintain known public interface for users Set Make Get Make Set Registration No. Various Behavior Various Behavior

  14. Object Oriented Programming Features • Classes are templates for objects, classes define properties and methods which dictate the state and behavior of the resulting software object • Data and related functions are contained within the same object space - encapsulation (data hiding) • Classes can be extended by creating subclasses which have similar properties and behaviors - inheritance • Inherited behaviors can be modified (overridden) to change the way an object responds to common messages- polymorphism • These three characteristics of OOP are often referred to as the“Object Oriented Paradigm”

  15. Class Structures and Methods

  16. Class Definition for a Vehicle (.as) public class Vehicle { // ------ constructor------ // public function Vehicle(){ } // end constructor // ------ properties ------ // var regNo:String; var make:String; var model:String; var saleValue:int;

  17. Set Method Definition (.as) // ------- set the registration number ------- // public function setRegNumber(aRegNumber:String):void { this.regNo = aRegNumber; } // end setRegNumber

  18. Get Method Definition (.as) // -------- get the registration number ------ // public function getRegNumber():String { var aRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber

  19. ActionScript 3.0 Implementation Details (.fla) varaCar:Vehicle = new Vehicle(); varreg:String; varmake:String; aCar.setRegNumber("BKD 1"); aCar.setMake("Ferrari"); // verify it works make = aCar.getMake(); reg = aCar.getRegNumber(); trace(make); trace(reg);

  20. Inheritance (subclassing) • A class can be extended by creating a subclass based on the class. The subclass will inherit the properties and behavior of the superclass from which it is based • Additional properties and behavior can be added to the subclass Multimedia Authoring B.Sc. Multimedia Computing

  21. Transport System Subclass Example Vehicle Superclass LeaseVehicle Subclass

  22. Transport System: Vehicle Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehicle Set the value of the vehicle Object: Vehicles Properties (attributes) Type Make Model Value Registration Number Any subclass of Vehicle will inherit all the properties and methods of the Vehicle class Multimedia Authoring B.Sc. Multimedia Computing

  23. Transport System: Vehicle and LeaseVehicle Functions (methods) Create an object of type Vehicle Set the Make of vehicle Get the make of vehicle Set the value of the vehicle Functions Create an object of type LeaseVehicle Set the leaseValue Get the leaseValue Set the leaseDate Get the LeaseDate Object: Vehicles Properties (attributes) Type Make Model Value Registration Number Subclass LeaseVehicle Properties LeaseValue LeaseDate

  24. Class Definition for LeaseVehicle public class LeaseVehicle extends Vehicle { // ------ constructor------ // public function LeaseVehicle(){ this.make = “Maserati”; this.regNo= “1 BKD”; } // end constructor // ------ properties ------ // var leaseValue:int; var leaseDate:Date;

  25. Class Definition for LeaseVehicle public class LeaseVehicle extends Vehicle { // ------ constructor------ // public function LeaseVehicle(){ this.regNo= "1 BKD"; this.make = "Maserati"; } // end constructor // ------ properties ------ // varleaseValue:int; varleaseDate:Date; } }

  26. Inherited Set Definitions // ------- not required as inherited from superclass ------- // public function setRegNumber(aRegNumber:String):void { this.regNo = aRegNumber; } // end setRegNumber

  27. Inherited Get Definitions // -------- not required as inherited from superclass ------ // public function getRegNumber():String { varaRegNumber:String; aRegNumber = this.regNo; return aRegNumber; } // end getRegNumber

  28. ActionScript 3.0 Implementation Details // instantiate a new LeaseVehicle object varaLeaseCar:LeaseVehicle = new LeaseVehicle(); //local variables varreg:String; varmake:String; // set the local variables by calling the inherited methods // from the Vehicle class make = aLeaseCar.getMake(); reg = aLeaseCar.getRegNumber(); // verify the data trace(make); trace(reg);

  29. Overriding Methods Subclassed methods can be overridden to modify their behavior for a given software design requirement that most reflects the inherited behaviour. The Vehicle class has a getSaleValue method which is not suited for a LeaseVehicle - lease vehicles are sold at the end of the lease period for a well below market value. Thus the getSaleValue method in the LeaseVehicle class could be overridden to reflect this modified behaviour.

  30. Vehicle Class getSaleValue Method public method getSaleValue():int{ var price:int; price = 12000; return price };

  31. Override Method for LeaseVehicle Class override public method getSaleValue():int{ var price:int; price = (12000 - discount); return price };

  32. Override Method for LeaseVehicle Class salePrice = aVehicle.getSaleValue() Gets the sale price for a Vehicle salePrice = aLeaseVehicle.getSaleValue() Gets the sale price – discount value for a Lease Vehicle as the getSaleValue method has been overidden (modified) in the LeaseVehicle class

  33. Access Modifiers Access to an object’s properties and methods by other objects I controlled by using an access modifier - public, private , or protected • public - available to all code • private- available only within the same class • protected - available within the same class and subclasses

  34. Summary • Encapsulation - data hiding • Inheritance - subclassing • Polymorphism - overriding behaviors • Access Modifiers - controlling access to objects and methods

More Related