Objective-C-类(static)方法、实例方法、overwrite(覆写)、属性(property)

来源:互联网 发布:益思商科留学 知乎 编辑:程序博客网 时间:2024/05/21 22:24

+表示类方法,-表示实例方法

总结一下几点:

1.类(static)方法

a. 类方法的调用

[类名称 类方法名称];

[Human toString]; 


注意:类方法
1,类方法可以调用类方法。
2,类方法不可以调用实例方法,但是类方法可以通过创建对象来访问实例方法。
3,类方法不可以使用实例变量。类方法可以使用self,因为self不是实例变量。
4,类方法作为消息,可以被发送到类或者对象里面去(实际上,就是可以通过类或者对象调用类方法的意思)。


2.实例方法

a.实例方法的调用

首先需要实例化该类

Human *man = [Human new]; 或者 Human *man = [Human alloc] init];


[类的实例  实例方法名称];

 [man showSex];   

注意:此处实例化该类时,调用了该类的构造函数init

并且该类的构造函数调用[super init]的返回值不等于 该类的self。

定义子类的实例

Woman *wife = [Woman new]; 

此处实例化该类时,调用了该类的构造函数init

并且该类的构造函数调用[super init]的返回值 等于 该类的self。

3.OverWrite方法(覆写,重载)

覆写父类的+(void)toString 

调用[Woman toString]; 


4.属性(property)

声明一个成员变量BOOL married; 

声明set方法-(void) setMarried:(BOOL)m; 

声明get方法-(BOOL) Married; 


或者可以使用关键字@property

@synthesize


定义一个Human父类

定义部分:

[html] view plaincopyprint?
  1. //  
  2. //  Human.h  
  3. //  OOP  
  4. //  
  5. //  Created by jimmy.yang on 11-2-9.  
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #import <Foundation/Foundation.h>  
  10.    
  11.    
  12. @interface Human : NSObject {  
  13.     BOOL sex;  
  14. }  
  15.    
  16. +(void) toString;  
  17.    
  18. -(void) showSex;  
  19.    
  20. @end  

注:+(void)前的加号,就表示这一个是类方法(static 方法),而-(void)表示这是一个实例方法

实现部分:

注意:下面的 -(id) init 即为构造函数.

[html] view plaincopyprint?
  1. //  
  2. //  Human.m  
  3. //  OOP  
  4. //  
  5. //  Created by jimmy.yang on 11-2-9.  
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #import "Human.h"  
  10.    
  11.    
  12. @implementation Human  
  13.    
  14. //构造函数  
  15. -(id) init  
  16. {  
  17.     NSLog(@"init() in Human is called");  
  18.     sex = TRUE;  
  19.     return(self);  
  20. }  
  21.    
  22. //static类方法  
  23. + (void)toString  
  24. {  
  25.     NSLog(@"this is a class method of Human");  
  26. }  
  27.    
  28.    
  29. //实例方法  
  30. - (void)showSex  
  31. {  
  32.     NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE");  
  33. }  
  34.    
  35.    
  36. @end  

再来定义一个Woman子类

定义部分:

[html] view plaincopyprint?
  1. //  
  2. //  Woman.h  
  3. //  OOP  
  4. //  
  5. //  Created by jimmy.yang on 11-2-9.  
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #import <Foundation/Foundation.h>  
  10. #import "Human.h"  
  11.    
  12.    
  13. @interface Woman : Human {  
  14.     BOOL married;  
  15. }  
  16.    
  17. -(void) canCook:(NSString*) foodName;  
  18.    
  19. -(void) setMarried:(BOOL)m;  
  20.    
  21. -(BOOL) Married;  
  22.    
  23. @end  

实现部分:

注意下面的:setMarried 与 Married 就是obj-C中属性的标准写法(当然以后还能看到其它简化的写法)

[html] view plaincopyprint?
  1. //  
  2. //  Woman.m  
  3. //  OOP  
  4. //  
  5. //  Created by jimmy.yang on 11-2-9.  
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  7. //  
  8.    
  9. #import "Woman.h"  
  10.    
  11.    
  12. @implementation Woman  
  13.    
  14. //Woman类的构造函数  
  15. -(id) init{  
  16.     NSLog(@"init() in Woman is called!");  
  17.     if (self==[super init]){  
  18.         sex = FALSE;  
  19.         married = FALSE;  
  20.     }  
  21.     return (self);  
  22. }  
  23.    
  24. //overwrite父类中的toString()  
  25. +(void)toString  
  26. {  
  27.     NSLog(@"This is Woman's ToString()");  
  28. }  
  29.    
  30. //Woman能做饭  
  31. -(void)canCook:(NSString*) foodName  
  32. {  
  33.     NSLog(@"I can cook %@",foodName);  
  34. }  
  35.    
  36. //属性的setter  
  37. -(void) setMarried:(BOOL)m  
  38. {  
  39.     married = m;  
  40. }  
  41.    
  42. //属性的getter  
  43. -(BOOL) Married  
  44. {  
  45.     return married;  
  46. }  
  47.    
  48. @end  

main方法中的调用:

[html] view plaincopyprint?
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3. #import "Woman.h"  
  4.    
  5. int main (int argc, const char * argv[]) {  
  6.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  7.    
  8.     // insert code here...  
  9.     NSLog(@"Hello, World!");  
  10.    
  11.     //调用类的“静态”方法  
  12.     [Human toString];  
  13.    
  14.     NSLog(@"----------------");  
  15.    
  16.     //创造一个Human的实例  
  17.     Human *man = [Human new];  
  18.    
  19.     //调用man的showSex方法  
  20.     [man showSex];  
  21.    
  22.     NSLog(@"----------------");  
  23.    
  24.     //定义一个Woman子类的实例  
  25.     Woman *wife = [Woman new];  
  26.     [wife canCook:@"Rice"];  
  27.    
  28.     //调用继承自父类的方法  
  29.     [wife showSex];  
  30.    
  31.     //设置属性  
  32.     [wife setMarried:TRUE];  
  33.    
  34.     //读取属性值  
  35.     NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE");  
  36.    
  37.     NSLog(@"----------------");  
  38.    
  39.     //调用overwrite后的toString方法  
  40.     [Woman toString];  
  41.    
  42.    
  43.     //Factory模式中常用的手法,在这里依然适用(只不过编译时会有警告 'Human' may not respond to '-canCook:')  
  44.     Human *wife2 = [Woman new];  
  45.     [wife2 canCook:@"soap"];  
  46.    
  47.    
  48.    
  49.     NSLog(@"----------------");  
  50.    
  51.     [pool drain];  
  52.     return 0;  
  53. }  

运行结果:

2011-02-09 17:01:02.016 OOP[1725:a0f] Hello, World!
2011-02-09 17:01:02.053 OOP[1725:a0f] this is a class method of Human
2011-02-09 17:01:02.062 OOP[1725:a0f] —————-
2011-02-09 17:01:02.075 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.091 OOP[1725:a0f] my sex is MALE
2011-02-09 17:01:02.094 OOP[1725:a0f] —————-
2011-02-09 17:01:02.099 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.104 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.105 OOP[1725:a0f] I can cook Rice
2011-02-09 17:01:02.108 OOP[1725:a0f] my sex is FEMALE
2011-02-09 17:01:02.109 OOP[1725:a0f] wife’s married = TRUE
2011-02-09 17:01:02.111 OOP[1725:a0f] —————-
2011-02-09 17:01:02.116 OOP[1725:a0f] This is Woman’s ToString()
2011-02-09 17:01:02.120 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.121 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.123 OOP[1725:a0f] I can cook soap
2011-02-09 17:01:02.125 OOP[1725:a0f] —————-