通過前邊的hello world程式,我們知道了如何包含標頭檔,以及NSLog的基本用法,從本節開始,通過一個簡單的類來講解如何聲明和定義類,以及類中的成員函數長什麼摸樣。
在第一節中描述的HOME中添加以下三個檔案Rec.h、Rec.c、main.m:
/*** @file Rec.h* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import <Foundation/Foundation.h>@interface Rec : NSObject { int length; int width;}-(void) setLength: (int) len setWidth: (int) wid;-(int) getLength;-(int) getWidth; +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr;@end
/*** @file Rec.m* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import "Rec.h"@implementation Rec-(void) setLength: (int) len setWidth: (int) wid{ NSLog(@"SET: Length=%d, Width=%d\n", len, wid);length = len;width = wid;}-(int) getLength{ return length;}-(int) getWidth{ return width;} +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr{ NSLog(@"This is NSString: %@ This is C String: %s\n", nsstr, cstr);}@end
/*** @file main.m* @brief * @author Don Hao* @date 2011-8-21 12:14:13* @version * <pre><b>copyright: </b></pre>* <pre><b>email: </b>hao.limin@gmail.com</pre>* <pre><b>company: </b>http://blog.csdn.net/donhao</pre>* <pre><b>All rights reserved.</b></pre>* <pre><b>modification:</b></pre>* <pre>Write modifications here.</pre>*/#import "Rec.h"int testCFun(int length, int width){ printf("C FUN: %d %d\n", length, width);}int main(int arvc, char* argv[]){ testCFun(1, 2); Rec* r = [Rec new]; [r setLength:10 setWidth:20]; NSLog(@"Length:%d\n", [r getLength]); NSLog(@"Width:%d\n", [r getWidth]); NSString* nsstr = @"NSSTRING"; const char* cstr = "CSTRING"; [Rec testStaticMethod:nsstr printCStr:cstr]; return 0;}
Makefile的內容(如果不清楚,請看第一節):
gen:gcc -o main main.m Rec.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base
運行結果如下:
下面及後續章節對相關內容逐一進行說明。
大家習慣於C/C++/C#/Java代碼風格的話,看到Objective-C的代碼難免覺得難以接受。無需有排斥的感覺,等看習慣了就好了。
本節先來將一下Objective-C是如何聲明和定義類的。
上邊是一個Rec類(矩形),我們將類的聲明放到了.h標頭檔(C++中類的定義可以放到.h中),類的定義放到了.m檔案中。
1. 類的聲明
類的聲明格式為:
@interface 類名 : 父類{ //屬性}//方法聲明@end
其中,該類必須直接或間接繼承自NSObject,@interface用於指示這是對類的定義,類名後邊的冒號是繼承(同C++),屬性放到大括弧中,在大括弧之後是類的方法,最後是@end(這個可以不寫,但為了讓其他人知道這個類定義到這裡就結束了,寫上更好)。
2. 類的定義
類的定義格式為:
@implementation 類名//方法定義@end
3 樣本說明
在上邊的樣本中對類Rec的定義:
a. 類Rec繼承自NSObject
b. 有兩個屬性:length和width
c. 有四個方法:具體的方法下節進行解釋。
d. 通過+/-來修飾方法,如果為-,則表明該方法屬於對象,如果為+,則表明該方法屬於類(即靜態方法)
Objective-C與C++的類的不同之處在於:
a. C++通過Class來標識類,而Objective-C通過@interface/@implementation來標識類;
b. C++通過static來區分是否是靜態方法,而Objective-C通過+/-來區分;
c. C++類的方法定義在類的大括弧中,而Objective-C在大括弧外;
e. C++對屬性和方法有public、private、protected等訪問限制屬性,而Objective-C中屬性為Protected,方法為Public;
f. C++支援多繼承,而Objective-C不支援多繼承;
一起來學Objective-C(1)——Window下開發環境安裝和Hello World
一起來學Objective-C(2)——Hello World深入
一起來學Objective-C(3)——如何聲明和定義類