Click here to load reader
Upload
atit-patumvan
View
1.315
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Chapter 8 : Inheritance
Citation preview
Object-Oriented Programming LanguageChapter 8 : Inheritance
Atit PatumvanFaculty of Management and Information Sciences
Naresuan University
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
It All Begins at the Root
2
NSObjectroot class or superclass
subclassFraction
@interface Fraction: NSObject :@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Subclasses and superclasses
3
NSObject
superclass
ClassA
@interface ClassA: NSObject{! int x;}
-(void) initVar;@end
@implementation ClassA-(void) initVar{! x = 100;}@end
ClassB
superclass
subclass
subclass @interface ClassB: ClassA-(void) printVar;@end
@implementation ClassB-(void) printVar{! NSLog(@"x= %i", x);}@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Subclasses and superclasses
4
@interface ClassA: NSObject{! int x;}
-(void) initVar;@end
@implementation ClassA-(void) initVar{! x = 100;}@end
@interface ClassB: ClassA-(void) printVar;@end
@implementation ClassB-(void) printVar{! NSLog(@"x= %i", x);}@end
NSObject
ClassA
ClassB
x
x
initVar
intVar printVar
Class Instance Variables Methods
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Simple Inheritance
5
:28: int main(int argc, char *argv[]) {29:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];30:!31:! ClassB * b = [[ClassB alloc] init];32:!33:! [b initVar];34:! [b printVar]; // x = 10035:!36:! [b release];37:!38:! [pool drain];39:! return 0;!40:! }41: } :
Program 8.1 main.m
@interface ClassA: NSObject{! int x;}
-(void) initVar;@end
@implementation ClassA-(void) initVar{! x = 100;}@end
@interface ClassB: ClassA-(void) printVar;@end
@implementation ClassB-(void) printVar{! NSLog(@"x= %i", x);}@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Extension Through Inheritance: Adding New Methods
6
01: #import <Foundation/Foundation.h>02:03: @interface Rectangle: NSObject04: {05:! int width;06:! int height;07: }08:09: @property int width, height;10:11: -(void) setWidth: (int) w andHeight: (int) h;12: -(int) area;13: -(int) perimeter;14:15: @end
Program 8.2 Rectangle.h
01: #import "Rectangle.h"02:03: @implementation Rectangle04:05: @synthesize width, height;06:07: -(void) setWidth: (int) w andHeight: (int) h08: {09:! width = w;10:! height = h;11: } : 21: @end
Program 8.2 Rectangle.m
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Extension Through Inheritance: Adding New Methods
7
01: #import <Foundation/Foundation.h>02:#import "Rectangle.h"03:04: int main(int argc, char *argv[]) {05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Rectangle * myRect = [[Rectangle alloc] init ];07:!08:! [myRect setWidth: 5 andHeight: 8];09:!10:! NSLog(@"Rectangle: w = %i, h = %i", myRect.width, myRect.height);11:! NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);12:!13:! [myRect release];14:! [pool drain];15:! return 0;16: }
Program 8.2 Rectangle.h
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Extension Through Inheritance: Subclassing
8
01: #import <Foundation/Foundation.h>02: #import "Rectangle.h"03:04: @interface Square : Rectangle05:06: -(void) setSide: (int) s;07: -(int) side;08: @end
Program 8.3 Square.h
01: #import "Square.h"02:03: @implementation Square: Rectangle04:05: -(void) setSide: (int) s06: {07:! [self setWidth: s andHeight: s];08: }09:10: -(int) side11: {12:! return width;!13: }14: @end
Program 8.3 Square.m
01: #import <Foundation/Foundation.h>02: #import "Square.h"03:04: int main (int argc, char * argv[])05: {06: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07: 08: Square * mySquare = [[Square alloc] init];09: 10: [mySquare setSide: 5];11: 12: NSLog(@"Square s = %i", [mySquare side]);13: NSLog(@"Area = %i, Perimeter = %i", [mySquare area], [mySquare perimeter]);14: 15: [pool drain];16: return 0;17: 18: }
Program 8.3 main.m
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
A Point Class and Memory Allocation
9
(0,0)
(x,y)
x
y myRect(x1,y1)
01: #import <Foundation/Foundation.h>02:03: @interface XYPoint: NSObject04: {05: int x;06: int y;07: }08: @property int x, y;09:10: -(void) setX: (int) xVal andY: (int) yVal;11: @end
Program 8.4 XYPoint.h : 05: @interface Rectangle: NSObject06: {07:! int width;08:! int height;09: XYPoint * origin;10: }11: :13: -(XYPoint *) origin; :
Program 8.4 Rectangle.h
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
The @class directive
10
01: #import <Foundation/Foundation.h>02:03: @class XYPoint;04:05: @interface Rectangle: NSObject06: {07:! int width;08:! int height;09: XYPoint * origin;10: }11:12: @property int width, height;13: -(XYPoint *) origin;14: -(void) setOrigin: (XYPoint *) pt;15: -(void) setWidth: (int) w andHeight: (int) h;16: -(int) area;17: -(int) perimeter;18:19: @end
Program 8.4 Rectangle.h
@class directive
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Handling Object in Method
11
01: #import "Rectangle.h"02:03: @implementation Rectangle04:05: @synthesize width, height;06:07: -(XYPoint *) origin08: {09: return origin;10: }11: -(void) setOrigin: (XYPoint *) pt12: {13: origin = pt;14: } :
Program 8.4 Rectangle.h01: #import <Foundation/Foundation.h>02:03: @class XYPoint;04:05: @interface Rectangle: NSObject06: {07:! int width;08:! int height;09: XYPoint * origin;10: }11:12: @property int width, height;13: -(XYPoint *) origin;14: -(void) setOrigin: (XYPoint *) pt;15: -(void) setWidth: (int) w andHeight: (int) h;16: -(int) area;17: -(int) perimeter;18:19: @end
Program 8.4 Rectangle.m
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Handling Object in Method
12
Program 8.4 main.m01: #import <Foundation/Foundation.h>02: #import "Rectangle.h"03: #import "XYPoint.h" : 09: Rectangle * myRect = [[Rectangle alloc] init];10: XYPoint * myPoint = [[XYPoint alloc] init];11: 12: [myPoint setX: 100 andY: 200];13: 14: [myRect setWidth: 5 andHeight: 8];15: myRect.origin = myPoint;16: 17: NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height);18: 19: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);20: 21: NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);22:23: [myRect release];24: [myPoint release]; :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
: 09: Rectangle * myRect = [[Rectangle alloc] init];10: XYPoint * myPoint = [[XYPoint alloc] init];11: 12: [myPoint setX: 100 andY: 200];13: 14: [myRect setWidth: 5 andHeight: 8];15: myRect.origin = myPoint;16: 17: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);18: 19: [myPoint setX: 50 andY: 50];20: 21: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);22: 23: [myRect release];24: [myPoint release];25: 26: [pool drain];27: return 0; :
Can you explain the output from Program 7.5?
13
Program 8.5 main.m
x = 100y = 200
myPoint
origin
myRect
Rectangle@xxxxXYPoint@yyyy
width = 5height = 8
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Fixing Reference Problem
14
:23: [[myRect origin] release]; 24: [myRect release];25: [myPoint release]; :
Program 8.5B Rectangle.m :12: -(void) setOrigin: (XYPoint *) pt13: {14: if (! origin)15: origin = [[XYPoint alloc] init];16: 17: origin.x = pt.x;18: origin.y = pt.y;19: } :
Program 8.5B main.m
x = 100y = 200
myPoint
origin
myRect
Rectangle@xxxx
XYPoint@yyyy
width = 5height = 8
x = 100y = 200
XYPoint@zzzz
pt
copy
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Overriding Methods
15
Program 8.6 main.m :07: @interface ClassA : NSObject {08: int x;09: }10:11: -(void) initVar;12: @end13:14: @implementation ClassA15: -(void) initVar16: {17: x = 100;18: }19:20: @end :
:41: ClassB * b = [[ClassB alloc] init];42: 43: [b initVar]; // uses overrinding method in B44: 45: [b printVar]; // reveal value of x; 46: [b release]; :
:22: @interface ClassB : ClassA23: -(void) initVar;24: -(void) printVar;25: @end26:27: @implementation ClassB28: -(void) initVar29: {30: x = 200;31: }32: -(void) printVar33: {34: NSLog(@"x = %i", x);35: }36: @end :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Overriding Methods: Which method is Selected?
16
Program 8.7 main.m (old) :07: @interface ClassA : NSObject {08: int x;09: }10:11: -(void) initVar;12: @end13:14: @implementation ClassA15: -(void) initVar16: {17: x = 100;18: }19:20: @end : :42: ClassA * a = [[ClassA alloc] init];43: ClassB * b = [[ClassB alloc] init];44: [a initVar]; // uses ClassA method45: [a printVar]; // reveal value of x46: 47: [b initVar]; // uses overrinding ClassB method48: [b printVar]; // reveal value of x; :
:12: -(void) printVar;13: @end14:15: @implementation ClassA16: -(void) initVar17: {18: x = 100;19: }20: -(void) printVar21: 22: NSLog(@"x = %i", x);23: } :
Program 8.7 main.m (new)
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Overriding the dealloc Method and the Keyword super
17
:23: [[myRect origin] release]; 24: [myRect release];25: [myPoint release]; :
Program 8.5B Rectangle.m :12: -(void) setOrigin: (XYPoint *) pt13: {14: if (! origin)15: origin = [[XYPoint alloc] init];16: 17: origin.x = pt.x;18: origin.y = pt.y;19: } :
Program 8.5B main.m
:33: -(void) dealloc34: {35: if(origin)36: [origin release];37: [super dealloc];38: } :
Program 8.7B Rectangle.m
: 23: [myRect release];24: [myPoint release]; :
Program 8.7B main.m
keyword super
Text
overriding dealloc method
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
Extension Through Inheritance: Adding New Instance Variables
18
:22: @interface ClassB : ClassA23: {24: int y;25: }26:27: -(void) initVar;28: -(void) printVar;29: @end30:31: @implementation ClassB32: -(void) initVar33: {34: x = 200;35: y = 300;36: }37: -(void) printVar38: {39: NSLog (@"x = %i", x);40: NSLog (@"y = %i", y);41: }42: @end :
Program 8.8 main.m :07: @interface ClassA : NSObject {08: int x;09:}10:11:-(void) initVar;12:13:@end14:15:@implementation ClassA16:-(void) initVar17:{18: x = 100;19:}20:@end :
Program 8.8 main.m
:48: ClassB * b = [[ClassB alloc] init];49: 50: [b initVar]; 51: [b printVar]; :
Text