Objective-C 语法一(类)

来源:互联网 发布:java数组如何调用方法 编辑:程序博客网 时间:2024/05/29 18:04

Objective-C 语法一(类)

首先申明下,本文为笔者学习《Objective-C 基础教程》的笔记,并加入笔者自己的理解和归纳总结。

1、#import语句

#import可保证头文件只被包含一次。
#import <Foundation/Foundation.h>

2、NSLog和@"字符串"

NSLog方法与printf相似,向控制台输出。
@符号表示NSString类型。

3、布尔类型(BOOL)

YES和NO

4、类

@interface表示这是类的接口,以@end结束。
@implementation实现类定义的接口,以@end结束。
每个类都是NSObject。
在{}内定义类需要的各种数据成员。
短线"-"声明是类方法,后面紧跟返回类型,void表示无返回值。
方法参数中()定义参数类型,紧跟参数名。

@interface ShapeBounds : NSObject {int width, height;}- (void) setWidth: (int)width height: (int)height;@end@implementation ShapeBounds- (void) setWidth: (int)w height: (int)h {width = w;height = h;}- (NSString*) description {return [NSString stringWithFormat: @"(%d, %d)", width, height];}@endint main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];NSLog(@"%@", bounds); // (100, 60)}

5、继承

类声明时,冒号":"后面的标识符是需要继承的类,Objective-C只支持单继承。
super关键字能调用父类的方法。

@interface Shape : NSObject - (void) draw;@end@implementation Shape- (void) draw {NSLog(@"draw Shape");}@end@interface Circle : Shape@end@implementation Circle- (void) draw {[super draw];NSLog(@"draw Circle");}@endint main(int argc, const char* argv[]) {Circle* circle = [Circle new];[circle draw];}

6、复合

Shape拥有自己的位置,可以用ShapeBounds表示。
@interface Shape : NSObject {ShapeBounds* bounds;}- (void) setBounds: (ShapeBounds*) bounds;- (void) draw;@end@implementation Shape- (void) setBounds: (ShapeBounds*) b {bounds = b;}- (void) draw {NSLog(@"draw Shape at %@", bounds);}@endint main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];Circle* circle = [Circle new];[circle setBounds: bounds];[circle draw];}

7、跨文件依赖

ShapeBounds可以拆分为ShapeBounds.h和ShapeBounds.m
ShapeBounds.h文件

#import <Foundation/Foundation.h>@interface ShapeBounds : NSObject {int width, height;}- (void) setWidth: (int)Width height: (int)height;@end
ShapeBounds.m文件
#import "ShapeBounds.h"@implementation ShapeBounds- (void) setWidth: (int)w height: (int)h {width = w;height = h;}- (NSString*) description {return [NSString stringWithFormat: @"(%d, %d)", width, height];}@end
Shape类分为Shape.h和Shape.m,在Shape中引用了ShapeBounds类,必须在文件中声明依赖关系。
@class可以声明引用关系,在编译时知道这是一个类,但ShapeBounds被修改时不用重新编译Shape.h文件。
Shape.h文件

#import <Foundation/Foundation.h>@class ShapeBounds;@interface Shape : NSObject {ShapeBounds* bounds;}- (void) setBounds: (ShapeBounds*) bounds;- (void) draw;@end
Shape.m文件
#import "Shape.h"#import "ShapeBounds.h"@implementation Shape- (void) setBounds: (ShapeBounds*) b {bounds = b;}- (void) draw {NSLog(@"draw Shape at %@", bounds);}@end
Circle类分为Circle.h和Circle.m,Circle.h文件中Shape类不能用@class声明。
Circle.h文件

#import "Shape.h"@interface Circle : Shape@end
Circle.m文件
#import "Circle.h"@implementation Circle- (void) draw {[super draw];NSLog(@"draw Circle");}@end
main.m文件
#import "Circle.h"#import "ShapeBounds.h"int main(int argc, const char* argv[]) {ShapeBounds* bounds = [ShapeBounds new];[bounds setWidth: 100 height: 60];Circle* circle = [Circle new];[circle setBounds: bounds];[circle draw];}

阅读全文
0 0