chapter 9 : polymorphism, dynamic typing, and dynamic binding

13
Object-Oriented Programming Language Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding Atit Patumvan Faculty of Management and Information Sciences Naresuan University

Upload: atit-patumvan

Post on 18-Dec-2014

891 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Object-Oriented Programming LanguageChapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit PatumvanFaculty of Management and Information Sciences

Naresuan University

Page 2: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

The Three Key Concepts

2

• Polymorphism: objects from different class can define methods that share the same name

• Dynamic Typing: the determination of the class that an object belongs to unit the program is executing.

• Dynamic Binding: the determination of the actual method to invoke an object unit program execution time

Page 3: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Polymorphism: Same Name, Different Class

3

01: #import "Complex.h"02:03: @implementation Complex04:05: @synthesize real, imaginary;06:07: -(void) print08: {09:! NSLog (@" %g + %gi ", real, imaginary);20: }21:22: -(void) setReal: (double) a andImaginary: (double) b23: {24:! real = a;25:! imaginary = b;26: }27:28: -(Complex *) add: (Complex *) f29: {30:! Complex *result = [[Complex alloc] init];31:! result.real = real + f.real;32:! result.imaginary = imaginary + f.imaginary;33:! return result;34: }35: @end

Program 9.1 main.m

Complex

real : double imaginary : double

print() : void setReal(double) : void andImaginary(double) : double add(Complex) : Complex

Page 4: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Polymorphism: Same Name, Different Class

4

Fraction

int : numerator int : denominator

print() : void setTo(int) over(int) : (void) convertToNum : (double) add(Fraction) : Fraction reduce() : void

Complex

real : double imaginary : double

print() : void setReal(double) : void andImaginary(double) : double add(Complex) : Complex

:38: -(Fraction *) add: (Fraction * ) f38: { 38:! Fraction * result = [[Fraction alloc] init]; 38:! result.numerator = numerator * f.denominator + denominator * f.numerator;38:! result.denominator = denominator * f.denominator;38:! [result reduce];38:38:! return result;38: } :

Program 9.1 Fraction.m

Page 5: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Polymorphism: Same Name, Different Class

5

Fraction

int : numerator int : denominator

print() : void setTo(int) over(int) : (void) convertToNum : (double) add(Fraction) : Fraction reduce() : void

Complex

real : double imaginary : double

print() : void setReal(double) andImaginary(double) : double add(Complex) : Complex

:38: -(Complex *) add: (Complex *) f38: {38:! Complex *result = [[Complex alloc] init];38:! result.real = real + f.real;38:! result.imaginary = imaginary + f.imaginary;38:! return result;38: } :

Program 9.1 Complex.m

Page 6: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Polymorphism: Same Name, Different Class

6

Fraction

int : numerator int : denominator

print() : void setTo(int) over(int) : (void) convertToNum : (double) add(Fraction) : Fraction reduce() : void

Complex

real : double imaginary : double

print() : void setReal(double) : andImaginary(double) : double add(Complex) : Complex

:09: Fraction *f1 = [[Fraction alloc] init];10: !! Fraction *f2 = [[Fraction alloc] init];11: !! Fraction *fracResult;12: !! Complex *c1 = [[Complex alloc] init];13: !! Complex *c2 = [[Complex alloc] init];14: !! Complex *compResult;15: !16: !! [f1 setTo: 1 over: 10];17: !! [f2 setTo: 2 over: 15];18: !19: !! [c1 setReal: 18.0 andImaginary: 2.5];20: !! [c2 setReal: -5.0 andImaginary: 3.2]; : !25: !! compResult = [c1 add: c2];26: !! [compResult print]; : !36: !! fracResult = [f1 add: f2];37: !! [fracResult print]; : !!

Program 9.1 main.m

Page 7: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Dynamic Binding and the id Type

7

:09:! id dataValue;10:! Fraction *f1 = [[Fraction alloc] init];11:! Complex *c1 = [[Complex alloc] init];12:13:! [f1 setTo: 2 over: 5];14:! [c1 setReal: 10.0 andImaginary: 2.5];15:16:! // first dataValue gets a fraction17:18:! dataValue = f1;19:! [dataValue print];20:21:! // now dataValue gets a complex number22:! dataValue = c1;22:! [dataValue print];23:!24:! [c1 release];25:! [f1 release]; :

Program 9.2 main.m

Fraction@yyyy

f1dataValue

Fraction@yyyy

numerator = 2denominator = 5

id

Fraction

int : numerator int : denominator

print() : void setTo(int) over(int) : (void) convertToNum : (double) add(Fraction) : Fraction reduce() : void

Page 8: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Dynamic Binding and the id Type

8

Fraction@yyyy

f1

f2

Fraction@yyyy

numerator = 2denominator = 5

id

Fraction@yyyy

Fraction

Fraction * f1 = [[Fraction alloc] init];id f2 = f1;

Page 9: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Argument and Return Types with Dynamic Typing

9

id add (id dataValue1, id dataValue2){ return [dataValue1 add: dataValue2];}

Fraction

int : numerator int : denominator

print() : void setTo(int) over(int) : (void) convertToNum : (double) add(Fraction) : Fraction reduce() : void

Complex

real : double imaginary : double

print() : void setReal(double) andImaginary(double) : double add(Complex) : Complex

Page 10: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Methods for Working with Dynamic Types

10

Method Question or Action

-(BOOL) isKindOfClass: class-object Is the object a member of class-object or a descendant?

-(BOOL) isMemberOfClass: class-object Is the object a member of class-object?

-(BOOL) respondsToSelector:selectorCan the object respond to the method specified by selector?

+(BOOL) instancesRespondToSelector: selector Can instances of the specified class respond to selector?

+(BOOL)isSubclassOfClass: class-object Is the object a subclass of the specified class?

-(id) performSelector: selector Apply the method specified by selector

-(id) performSelector: selector withObject: objectApply the method specified by selector, passing the argument object.

-(id) performSelector: selector withObject: object1 withObject: object2

Apply the method specified by selector with the arguments object1 and object2.

Page 11: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Exception Handling Using @try

11

01: #import "Fraction.h"02:03: int main (int argc, char *argv [])04: {05: !NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Fraction *f = [[Fraction alloc] init];07:! [f noSuchMethod];08:! NSLog (@"Execution continues!");09:! [f release];10:! [pool drain];11:! return 0;12: }

Program 9.4 main.m

$ ./main2012-04-08 12:36:14.589 main[4336:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10be0c2102012-04-08 12:36:14.590 main[4336:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10be0c210'*** First throw call stack:(! 0 CoreFoundation 0x00007fff91708fc6 __exceptionPreprocess + 198! 1 libobjc.A.dylib 0x00007fff9344dd5e objc_exception_throw + 43! 2 CoreFoundation 0x00007fff917952ae -[NSObject doesNotRecognizeSelector:] + 190! 3 CoreFoundation 0x00007fff916f5e73 ___forwarding___ + 371! 4 CoreFoundation 0x00007fff916f5c88 _CF_forwarding_prep_0 + 232! 5 main 0x000000010bd37bf2 main + 194! 6 main 0x000000010bd376d4 start + 52)terminate called throwing an exceptionAbort trap: 6

Page 12: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

@try and @ catch Statement

12

@try { statement statement ...}@catch (NSException *exception) { statement statement ...}

throw exception

Exception

catch exception

Page 13: Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

Exception Handling Using @try

13

01: #import "Fraction.h"02:03: int main (int argc, char *argv [])04: {05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Fraction *f = [[Fraction alloc] init];07:08:! @try {09:! ! [f noSuchMethod];10:! }11:! @catch (NSException *exception) {12:! ! NSLog(@"Caught %@%@", [exception name], [exception reason]);13:! }14:! NSLog (@"Execution continues!");15:! [f release];16:! [pool drain];17:! return 0;18: }

Program 9.5 main.m

$ ./main2012-04-08 12:45:32.562 main[4445:707] -[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c2102012-04-08 12:45:32.564 main[4445:707] Caught NSInvalidArgumentException-[Fraction noSuchMethod]: unrecognized selector sent to instance 0x10870c2102012-04-08 12:45:32.564 main[4445:707] Execution continues!$