Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция.

Similar presentations


Presentation on theme: "Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция."— Presentation transcript:

1 Objective C

2 Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция

3 id Специальный тип, являющийся указателем на произвольный объект. [receiver message]; nil может принимать любые сообщения, возвращает тоже nil

4 Сообщения [myRect setOrigin:30.0 :50.0]; [myRect setWidth:10.0 height:20.0]; [myObject makeGroup: obj1, obj2, obj3, obj4, nil]; [myRect setColor:[otherRect color]]; [Rect newrect]; Rect *rect = [Rect new];

5 Создание новых классов @interface ClassName : SuperClass { instance variable declarations } method declarations @end

6 @interface Rect : NSObject { float width; float height; BOOL isFilled; NSColor * color; } @end

7 @interface Rect : NSObject { float x, y; float width; float height; BOOL isFilled; NSColor * color; } + newRect; - (void) display; - (float) width; - (float) height; - (float) area; - (void) setWidth: (float) theWidth; - (void) setHeight: (float) theHeight; - (void) setX: (float) theX y: (float) theY; @end

8 #import "Rect.h" @implmentation Rect + newRect { Rect * rect = [[Rect alloc] init]; [rect setWidth: 1.0f]; [rect setHeight: 1.0f]; [rect setX: 0.0f y: 0.0f]; return [rect autorelease]; } - (float) width { return width; } - (float) height { return height; } - (float) area { return [self width] * [self height]; } - (void) setWidth: (float) theWidth { width = theWidth; } - (void) setHeight: (float) theHeight { height = theHeight; } - (void) setX: (float) theX y: (float) theY { x = theX; y = theY; } @end

9 @interface Worker : NSObject { char * name; @private int age; char * evaluation; @protected int job; float wage; @public id boss }

10 Как работают сообщения [object msg] -> objc_msgSend id objc_msgSend(id theReceiver, SEL theSelector,...) object -> isa -> class object -> superclass, dispatch table

11

12 Селекторы SEL setWidth = @selector(setWidth:); SEL setPos = @selector(setX:y:); SEL setWidth = NSSelectorFromString ( @"setWidth:" ); NSString * methodName = NSStringForSelector ( setPos ); if ( [anObject respondsToSelector: @selector(setWidth:)] ) [anObject setWidth:1];

13 self super

14

15 Протоколы @protocol ProtocolName method declarations @end @protocol Serializable - (id) initWithCoder: (NSCoder *) coder; - (void) encodeWithCoder: (NSCoder *) coder; @end

16 Протоколы @protocol MyProto if ( [myObject conformsToProtocol: @protocol (Serializable)] ) [myObject encodeWithCoder: myCoder]; id myObject; Shape *myObject;

17 Создание и уничтожение объектов id anObject = [[Rectangle alloc] init]; id anObject = [Rectangle alloc]; [anObject init]; id anObject = [Rectangle new];

18 init - initWithName: (const char *) theName // designated initializer { [super init]; // call inherited method name = strdup ( theName ); } - init { return [self initWithName: ""]; }

19 retain, release id object = [MyClass new]; … [object release]; id object = [[object getResult] retain]; … [object release];

20 dealloc - (void) dealloc {... [super dealloc]; }

21 NSAutoreleasePool NSAutoreleasePool *pool = [NSAutoreleasePool new]; … [pool release];

22 autorelease + (Rect *) newRect { Rect *rect = [Rect new]; [rect autorelease]; return rect; }


Download ppt "Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция."

Similar presentations


Ads by Google