一起来学Objective-C(3)——如何声明和定义类

来源:互联网 发布:西安淘宝设计代运营 编辑:程序博客网 时间:2024/05/01 17:10

通过前边的hello world程序,我们知道了如何包含头文件,以及NSLog的基本用法,从本节开始,通过一个简单的类来讲解如何声明和定义类,以及类中的成员函数长什么摸样。

在第一节中描述的HOME中添加以下三个文件Rec.h、Rec.c、main.m:

[cpp] view plaincopy
  1. /** 
  2. * @file Rec.h 
  3. * @brief  
  4. * @author Don Hao 
  5. * @date 2011-8-21 12:14:13 
  6. * @version  
  7. * <pre><b>copyright: </b></pre> 
  8. * <pre><b>email: </b>hao.limin@gmail.com</pre> 
  9. * <pre><b>company: </b>http://blog.csdn.net/donhao</pre> 
  10. * <pre><b>All rights reserved.</b></pre> 
  11. * <pre><b>modification:</b></pre> 
  12. * <pre>Write modifications here.</pre> 
  13. */  
  14.   
  15. #import <Foundation/Foundation.h>  
  16.   
  17. @interface Rec : NSObject   
  18. {  
  19.     int length;  
  20.     int width;  
  21. }  
  22.   
  23. -(void) setLength: (int) len setWidth: (int) wid;  
  24. -(int) getLength;  
  25. -(int) getWidth;        
  26. +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr;  
  27. @end  

 

[cpp] view plaincopy
  1. /** 
  2. * @file Rec.m 
  3. * @brief  
  4. * @author Don Hao 
  5. * @date 2011-8-21 12:14:13 
  6. * @version  
  7. * <pre><b>copyright: </b></pre> 
  8. * <pre><b>email: </b>hao.limin@gmail.com</pre> 
  9. * <pre><b>company: </b>http://blog.csdn.net/donhao</pre> 
  10. * <pre><b>All rights reserved.</b></pre> 
  11. * <pre><b>modification:</b></pre> 
  12. * <pre>Write modifications here.</pre> 
  13. */  
  14.   
  15. #import "Rec.h"  
  16.   
  17. @implementation Rec  
  18.   
  19. -(void) setLength: (int) len setWidth: (int) wid  
  20. {  
  21.     NSLog(@"SET: Length=%d, Width=%d\n", len, wid);  
  22.     length = len;  
  23.     width = wid;  
  24. }  
  25.   
  26. -(int) getLength  
  27. {  
  28.     return length;  
  29. }  
  30.   
  31. -(int) getWidth  
  32. {  
  33.     return width;  
  34. }   
  35.   
  36. +(void) testStaticMethod:(NSString*) nsstr printCStr:(const char*) cstr  
  37. {  
  38.     NSLog(@"This is NSString: %@  This is C String: %s\n", nsstr, cstr);  
  39. }  
  40. @end  
[cpp] view plaincopy
  1. /** 
  2. * @file main.m 
  3. * @brief  
  4. * @author Don Hao 
  5. * @date 2011-8-21 12:14:13 
  6. * @version  
  7. * <pre><b>copyright: </b></pre> 
  8. * <pre><b>email: </b>hao.limin@gmail.com</pre> 
  9. * <pre><b>company: </b>http://blog.csdn.net/donhao</pre> 
  10. * <pre><b>All rights reserved.</b></pre> 
  11. * <pre><b>modification:</b></pre> 
  12. * <pre>Write modifications here.</pre> 
  13. */  
  14.   
  15. #import "Rec.h"  
  16.   
  17. int testCFun(int length, int width)  
  18. {  
  19.     printf("C FUN: %d %d\n", length, width);  
  20. }  
  21.   
  22. int main(int arvc, char* argv[])  
  23. {  
  24.     testCFun(1, 2);  
  25.       
  26.     Rec* r = [Rec new];  
  27.       
  28.     [r setLength:10 setWidth:20];  
  29.       
  30.     NSLog(@"Length:%d\n", [r getLength]);  
  31.     NSLog(@"Width:%d\n", [r getWidth]);  
  32.       
  33.     NSString* nsstr = @"NSSTRING";  
  34.     const char* cstr = "CSTRING";  
  35.       
  36.     [Rec testStaticMethod:nsstr printCStr:cstr];  
  37.     return 0;  
  38. }  

Makefile的内容(如果不清楚,请看第一节):

[plain] view plaincopy
  1. gen:  
  2.     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. 类的声明

类的声明格式为:

[plain] view plaincopy
  1. @interface 类名 : 父类  
  2. {  
  3.     //属性  
  4. }  
  5.   
  6. //方法声明  
  7.   
  8. @end  

    其中,该类必须直接或间接继承自NSObject,@interface用于指示这是对类的定义,类名后边的冒号是继承(同C++),属性放到大括弧中,在大括弧之后是类的方法,最后是@end(这个可以不写,但为了让其他人知道这个类定义到这里就结束了,写上更好)。

2. 类的定义

类的定义格式为:

[plain] view plaincopy
  1. @implementation 类名  
  2.   
  3. //方法定义  
  4.   
  5. @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)——如何声明和定义类

原创粉丝点击