Download presentation
Presentation is loading. Please wait.
Published byArron Wiggins Modified over 8 years ago
1
CUSTOM OOP
2
SYNTAX OF A CLASS DEFINITION Class definitions (descriptions) look like this: class ClassName { Descriptions of the instance variables and methods each object will have and the constructors that initialize a new object. }
3
Often programmers separate the definition into three sections: class ClassName { // Description of the variables. // Description of the constructors. // Description of the methods. }
4
A TINY EXAMPLE package { import mx.controls.*; public class HelloObject { // method definition public function speak():void { Alert.show( "Hello from an object!"); } var anObject:HelloObject = new HelloObject(); anObject.speak();
5
If the class definition does not include a constructor a default constructor is automatically supplied by the ACTIONSCRIPT compiler. It’s good programming habit to include a default on yourself package { import mx.controls.*; public class HelloObject { //constructors in AS are like functions but without return values and have the same name as the class public function HelloObject() { } // method definition function speak():void { Alert.show( "Hello from an object!"); } var anObject:HelloObject = new HelloObject(); anObject.speak();
6
METHODS CAN USE VARIABLES If different instances (different objects) of the HelloObject class print different strings, then each instance must have its own data, which must be initialized. Initializing the instance variables of an object is done through its constructor
7
IMPROVED CLASS Here is what we have so far for HelloObject(I’ve removed the package statements as well as some other parts to make things simpler to see): class HelloObject { function speak():void { Alert.show("Hello from an object!"); } Here is a start on improving HelloObject: class HelloObject { __________ _____________ ; // keep the object's message here function speak():void { Alert.show( _________________ ); // print the object's message }
8
USING A CONSTRUCTOR Here is the class with a String reference variable included. Of course, you may have used a different name for it. class HelloObject { var greeting:String; function speak():void{ Alert.show( greeting ); } The class is not complete because there is no way to initialize the greeting
9
COMPLETED CONSTRUCTOR class HelloObject{ var greeting:String; function HelloObject(st:String) { greeting = st; } function speak():void { Alert.show(greeting); } var anObject:HelloObject = new HelloObject("A Greeting!"); anObject.speak();
10
FILL IN THE BLANKS 1. Fill in the blanks so that the following program writes "Hello Vesta" six times to the monitor. class VestaHello{ public function main ( ):void { var j:Number = 0 ; while ( j < 6 ) { Alert.show( "Hello Vesta" ); j = j + 1; } This program shows little of the object oriented style of programming. For the task at hand it is probably the best of all ten programs since it accomplishes the task in a simple and clear manner. But let us look at some other programs.
11
2. Now write the program so that it makes use of a Vesta object that writes "Hello Vesta" to the monitor. class Vesta{ function speak():void { Alert.show( “Hello Vesta” ); } class VestaHello2{ public function main ( ):void { int j = 0 ; var vest:Vesta = new Vesta(); while ( j < 6 ) { vest.speak(); j = j + 1; } In this program one Vesta object is created, whose speak() method is invoked size times. If all that you wanted to do was to write the phrase six times, the first version of the program is probably better than this version.
12
3. Here is a modification of the program. Now six Vesta objects are created, and each one uses its speak() method once. class Vesta{ function speak():void { Alert.Show( "Hello Vesta" ); } class VestaHello3{ public function main ( ):void { int j = 0 ; while ( j < 6 ) { var vest:Vesta = new Vesta() ; vest.speak(); j = j + 1; } This version of the program is less efficient than the previous program because this version creates six objects all of which do the same thing, and each of which immediately becomes garbage after it is used. This is a poor programming style.
13
4. Here is a further modification of the program. Again six Vesta objects are created, but now each one is a temporary, unnamed object. class Vesta{ function speak():void { Alert.show( "Hello Vesta" ); } class VestaHello4{ public function main ( ):void { int j = 0 ; while ( j < 6 ) { new Vesta().speak() ; j = j + 1; } The new operator is used to create each object and each newly created object's speak() method is invoked. In spite of its sophistication, this version of the program is about as impractical as the previous version.
14
5. Now the Vesta class is modified so a Vesta object writes out its message six times: class Vesta { function speak():void { int j = 0 ; while ( j < 6 ) { Alert.show(“Hello Vesta"); j = j + 1; } class VestaHello5{ public function main ( ):void { var vest:Vesta = new Vesta() ; vest.speak(); } This version has the advantage that only one object is created, and has the further advantage that main() is now a very simple program. If main() is expected to be become longer as more and more things are added to it, the Vesta object might be sensible. But for the simple task as stated, the first program is still the best.
15
6. In this version the class definition includes a constructor that initializes the message to be printed: class Vesta{ var message:String ; // The string to be printed // function Vesta(mess:String ) { message = mess ; // Initialize the message } function speak():void { int j = 0 ; while ( j < 6 ) { Alert.show(message); j = j + 1; } class VestaHello6{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta" ) ; vest. speak() ; } Again, whether the Vesta class is sensible depends on the task. If the task is complicated, and the above program is just a small step toward a complete solution, then maybe the Vesta class is a good design.
16
7. Further sophistication is to include the iteration limit in the constructor. class Vesta{ var message:String ; // The string to be printed var limit:int ; // Number of times to print // Constructor Function Vesta(mess:String, lim:Number ) { message = mess ; // Initialize the message limit = lim ; // Initialize the limit } function speak():void { int j = 0 ; while ( j < limit ) { Alert.show( message ); j = j + 1; } class VestaHello7{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta", 6) ; vest. speak() ; } This version of the Vesta class is more flexible than the previous version. It is probably better since it can be used for more situations than the previous.
17
8. On further thought (which should have been done before any coding was done) it seems more sensible that the number of times to write the message is better specified as a parameter to the speak() method. This further increases the flexibility of the class. class Vesta{ var message:String ; // The string to be printed // Constructor function Vesta( mess:String ) { message = mess ; // Initialize the message } function speak( i:int ):void { int j = 0 ; while ( j < i ) { Alert.show( message ); j = j + 1; } class VestaHello8{ public function main ( ):void { var vest:Vesta = new Vesta ( "Hello Vesta" ) ; vest. speak( 6 ) ; } This version seem more logical, as well as being more versatile. The number of times a message should be repeated should be a characteristic of the request, not an inherent characteristic of the object.
18
9. Mostly a Vesta object says "Hello Vesta" each time it is used. If that is what we want, the main() method should not have to say so. Classes can have several constructors that each take different parameters (remember the Point class.) In the following version of the program there are two constructors, one automatically initializes the message to "Hello Vesta" and the other initializes the message to the requested message. class Vesta{ var message:String ; // The string to be printed // Parameter-less Constructor function Vesta( ) { message = “Hello Vesta" ; // Initialize the message } // One Parameter Constructor function Vesta(mess:String) { message = mess; // Initialize the message } function speak( int limit ):void { int j = 0 ; while ( j < limit ) { Alert.show( message ); j = j + 1; } class VestaHello9{ public function main ( ):void { var vest:Vesta = new Vesta () ; // Use the default value "Hello Vesta“ vest. speak(6) ; }
19
10. A further improvement to the Vesta class is to include a method that changes the message string. This is NOT a constructor---it is a method that is part of an existing object and changes that object's message. class Vesta{ String message ; // The string to be printed // Parameter-less Constructor Function Vesta( ) { message = "Hello Vesta" ; // Initialize the message } // One Parameter Constructor fuction Vesta(mess:String ) { message = mess ; // Initialize the message } // Method that changes this object's message function changeMessage(mess:String):void { message = mess ; // Initialize the message } function speak( int limit ):void { int j = 0 ; while ( j < limit ) { Alert.show(message ); j = j + 1; } class VestaHello10{ public function main ( ):void { var vest:Vesta = new Vesta () ; // Use the default value "Hello Vesta" vest. speak( 6 ) ; // Change the message to "Hello Ceres" vest.changeMessage( “ Hello Ceres " ); vest. speak( 6 ) ; }
20
PROGRAMMING EXERCISES Exercise 1 Modify the program in this ppt so that a HelloObject object writes out the greeting as many times as there are characters in the greeting. The HelloObject class will have a constructor that allows it to initialize objects to different greetings. HelloHelloHelloHelloHello class HelloObject{ var greeting:String; function HelloObject(st:String ) { for (var i:int = 0; i < st.length; i++) { greeting += st; } function speak():void { Alert.show( greeting ); }
21
Exercise 2 Modify the program so that class HelloObject has two greeting messages: a morning greeting and an evening greeting. There will two output methods, one for each greeting. Good morning World!Good evening World! class HelloObject{ var greeting:String; function morning() { greeting = “Good morning World!”; } function evening() { greeting = “Goog evening World!”; } function speak():void { Alert.show( greeting ); }
22
Exercise 3 Write a version of the HelloObject program where the greeting that is printed by the object is given by the user: Enter Greeting:Hello Mars! Hello Mars! class HelloObject{ function speak(input:String):void { Alert.show(input); }
23
Exercise 4 Add another constructor to the HelloObject class that takes a HelloObject object as a parameter: function HelloObject( init:HelloObject){ // initialize the new object's greeting to the same as that of the init parameter this.init = init; } The additional constructor will not alter its parameter (of course), merely use its data. Use "dot notation" to refer to the String inside the parameter.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.