2. 2
It All Begins at the Root
@interface Fraction: NSObject
:
@end
NSObject
root class or superclass
Fraction
subclass
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
3. 3
Subclasses and superclasses
@interface ClassA: NSObject
{
! int x;
NSObject }
-(void) initVar;
@end
subclass superclass @implementation ClassA
-(void) initVar
{
! x = 100;
ClassA }
@end
@interface ClassB: ClassA
subclass superclass -(void) printVar;
@end
@implementation ClassB
-(void) printVar
ClassB {
! NSLog(@"x= %i", x);
}
@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
4. 4
Subclasses and superclasses
@interface ClassA: NSObject
{
! int x;
Class Instance Variables Methods
}
-(void) initVar; NSObject
@end
@implementation ClassA
-(void) initVar
{
! x = 100;
}
@end ClassA x initVar
@interface ClassB: ClassA
-(void) printVar;
@end
@implementation ClassB
-(void) printVar
{ ClassB x intVar printVar
! NSLog(@"x= %i", x);
}
@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
5. 5
Simple Inheritance
Program 8.1 main.m
:
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 = 100
35:!
36:! [b release];
@interface ClassA: NSObject @interface ClassB: ClassA
37:!
{ -(void) printVar;
38:! [pool drain];
! int x; @end
39:! return 0;!
}
40:! }
@implementation ClassB
41: }
-(void) initVar; -(void) printVar
:
@end {
! NSLog(@"x= %i", x);
@implementation ClassA }
-(void) initVar @end
{
! x = 100;
}
@end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
6. 6
Extension Through Inheritance: Adding New Methods
Program 8.2 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @interface Rectangle: NSObject
04: {
05:! int width;
06:! int height;
07: }
08:
09: @property int width, height;
10:
11: -(void) setWidth: (int) w andHeight: (int) h; Program 8.2 Rectangle.m
12: -(int) area;
13: -(int) perimeter; 01: #import "Rectangle.h"
14: 02:
15: @end 03: @implementation Rectangle
04:
05: @synthesize width, height;
06:
07: -(void) setWidth: (int) w andHeight: (int) h
08: {
09:! width = w;
10:! height = h;
11: }
:
21: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
7. 7
Extension Through Inheritance: Adding New Methods
Program 8.2 Rectangle.h
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: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
8. 8
Extension Through Inheritance: Subclassing
Program 8.3 Square.h Program 8.3 main.m
01: #import <Foundation/Foundation.h> 01: #import <Foundation/Foundation.h>
02: #import "Rectangle.h" 02: #import "Square.h"
03: 03:
04: @interface Square : Rectangle 04: int main (int argc, char * argv[])
05: 05: {
06: -(void) setSide: (int) s; 06: NSAutoreleasePool * pool =
07: -(int) side; [[NSAutoreleasePool alloc] init];
08: @end 07:
08: Square * mySquare = [[Square alloc] init];
Program 8.3 Square.m 09:
10: [mySquare setSide: 5];
01: #import "Square.h" 11:
02: 12: NSLog(@"Square s = %i", [mySquare side]);
03: @implementation Square: Rectangle 13: NSLog(@"Area = %i, Perimeter = %i",
04: [mySquare area], [mySquare perimeter]);
05: -(void) setSide: (int) s 14:
06: { 15: [pool drain];
07:! [self setWidth: s andHeight: s]; 16: return 0;
08: } 17:
09: 18: }
10: -(int) side
11: {
12:! return width;!
13: }
14: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
9. 9
A Point Class and Memory Allocation
(x,y)
y myRect
(x1,y1)
(0,0) x
Program 8.4 XYPoint.h Program 8.4 Rectangle.h
01: #import <Foundation/Foundation.h> :
02: 05: @interface Rectangle: NSObject
03: @interface XYPoint: NSObject 06: {
04: { 07:! int width;
05: int x; 08:! int height;
06: int y; 09: XYPoint * origin;
07: } 10: }
08: @property int x, y; 11:
09: :
10: -(void) setX: (int) xVal andY: (int) yVal; 13: -(XYPoint *) origin;
11: @end :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
10. 10
The @class directive
Program 8.4 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @class XYPoint;
04:
@class directive
05: @interface Rectangle: NSObject
06: {
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
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language