170 likes | 227 Views
CISC 110 Day 4. Object-oriented Programming (OOP). Outline. Typecasting (continued from last day) Introduction to OOP Classes Setting properties Using methods Applying methods to Objects Parameters to provide options. Typecasting.
E N D
CISC 110Day 4 Object-oriented Programming (OOP)
Outline • Typecasting (continued from last day) • Introduction to OOP • Classes • Setting properties • Using methods • Applying methods to Objects • Parameters to provide options
Typecasting Typecasting is the process of converting a value of one data type into a value of another data type. Example: var i:int = 100; var s:String = String(i); trace(s); Output: 100
Object-oriented Programming Object-oriented Programming (OOP)is a programming paradigm that uses variables called objects to manipulate other objects. Related Terms: -Information hiding -Data abstraction -Encapsulation -Modularity -Polymorphism -Inheritance 4
Library System Example Tasks: Borrow a book Return a book Search book lists by title, author or subject Issue new library card Check library card for books borrowed Obtain address of library card owner Add a new book to library Remove an old book Add a new employee to library staff Remove an employee (they’ve left for a job in computing science) 5
Library System Example Main Objects: Book Borrow Return Add new book Remove old book Search book list • Employee • Add a new employee to library staff • Remove an employee • Card Holder • Issue new card • Check for books borrowed • Obtain address 6
OO Terminology Objects: Data items manipulated by a computer program e.g. an MP3 Player Methods: Operations performed by (or on) an object e.g. pause Attributes: Characteristics that describe the state of an object (properties) e.g. the song now playing 7
OO Terminology Class A template that describes the methods and attributes of a group of objects e.g. description of iPod nanos The group of all objects that fit that description, each of which is an instanceof the class e.g. the group of all the 321,520,001 iPod nanos in the world 8
Hierarchy of Classes Generic MP3 Player Class is a is a is a SanDisk Sansa Fuze Class iPod nano Class Microsoft Zune Class … … … Sansa Fuze object instances iPod nano object instances Zune object instances An iPod “is a” MP3 Player 9
OO Terminology Inheritance:A subclass can “inherit” the characteristics and capabilities of a class above it in the hierarchy without redefining them, so that their common properties only need to be defined once. e.g. Generic CD class common methods: ‘play’,‘pause’, etc. e.g. Sansa Fuze extra method: ‘shuffle’ 10
Creating Object Instanceswith a Constructor Method Example: var myCircle: MovieClip; // Creates a variable of type MovieClip myCircle = new MovieClip( ); // Creates an instance of MovieClip General Syntax: var <variable name> : <class>; <variable name> = new <class> ( ); 11
Constructor Methods Every class for creating objects has a constructor method. The constructor method sets the initial values of the attributes of the new object. 12
Some MovieClip Properties width height x y Width of a MovieClip Height of a MovieClip x-coordinate position of a MovieClip y-coordinate position of a MovieClip 13
Some MovieClip Methods play() stop() nextFrame() goToAndPlay(x) goToAndStop(x) Tells playhead to play a MovieClip’s timeline from its current frame location Tells playhead to stop at a frame in a MovieClip’s timeline Tells playhead to advance to the next frame and stop in a MovieClip’s timeline Tells MovieClip to move the playhead and play from a specific frame on its timeline Tells MovieClip to move the playhead and stop at a specific frame on its timeline 14
Syntax for Setting a Property myCircle.height = 20; semi-colon object’s property assignment property variable name operator (=) value name period 15
Syntax for Calling a Method myCircle.play( ); semi-colon object’s method parentheses variable name (may be parameters) name period 16
Setting Attributes and Using Methods Examples: myCircle.x = 40; parameter myCircle.height = 30; myCircle.goToAndPlay( 10 ); Parametersprovide options, so a method can do variations of an operation, not always the same thing. 17