Objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(property)...
先來(lái)定義一個(gè)Human父類
定義部分:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // // Human.h // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // ? #import <Foundation/Foundation.h> ? ? @interface Human : NSObject { BOOL sex; } ? +(void) toString; ? -(void) showSex; ? @end |
注:+(void)前的加號(hào),就表示這一個(gè)是類方法(static 方法),而-(void)表示這是一個(gè)實(shí)例方法
實(shí)現(xiàn)部分:
注意:下面的 -(id) init 即為構(gòu)造函數(shù).
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // // Human.m // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // ? #import "Human.h" ? ? @implementation Human ? //構(gòu)造函數(shù) -(id) init { NSLog(@"init() in Human is called"); sex = TRUE; return(self); } ? //static類方法 + (void)toString { NSLog(@"this is a class method of Human"); } ? ? //實(shí)例方法 - (void)showSex { NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE"); } ? ? @end |
再來(lái)定義一個(gè)Woman子類
定義部分:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // // Woman.h // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // ? #import <Foundation/Foundation.h> #import "Human.h" ? ? @interface Woman : Human { BOOL married; } ? -(void) canCook:(NSString*) foodName; ? -(void) setMarried:(BOOL)m; ? -(BOOL) Married; ? @end |
實(shí)現(xiàn)部分:
注意下面的:setMarried 與 Married 就是obj-C中屬性的標(biāo)準(zhǔn)寫(xiě)法(當(dāng)然以后還能看到其它簡(jiǎn)化的寫(xiě)法)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // // Woman.m // OOP // // Created by jimmy.yang on 11-2-9. // Copyright 2011 __MyCompanyName__. All rights reserved. // ? #import "Woman.h" ? ? @implementation Woman ? //Woman類的構(gòu)造函數(shù) -(id) init{ NSLog(@"init() in Woman is called!"); if (self==[super init]){ sex = FALSE; married = FALSE; } return (self); } ? //overwrite父類中的toString() +(void)toString { NSLog(@"This is Woman's ToString()"); } ? //Woman能做飯 -(void)canCook:(NSString*) foodName { NSLog(@"I can cook %@",foodName); } ? //屬性的setter -(void) setMarried:(BOOL)m { married = m; } ? //屬性的getter -(BOOL) Married { return married; } ? @end |
main方法中的調(diào)用:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #import <Foundation/Foundation.h> #import "Human.h" #import "Woman.h" ? int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; ? // insert code here... NSLog(@"Hello, World!"); ? //調(diào)用類的“靜態(tài)”方法 [Human toString]; ? NSLog(@"----------------"); ? //創(chuàng)造一個(gè)Human的實(shí)例 Human *man = [Human new]; ? //調(diào)用man的showSex方法 [man showSex]; ? NSLog(@"----------------"); ? //定義一個(gè)Woman子類的實(shí)例 Woman *wife = [Woman new]; [wife canCook:@"Rice"]; ? //調(diào)用繼承自父類的方法 [wife showSex]; ? //設(shè)置屬性 [wife setMarried:TRUE]; ? //讀取屬性值 NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE"); ? NSLog(@"----------------"); ? //調(diào)用overwrite后的toString方法 [Woman toString]; ? ? //Factory模式中常用的手法,在這里依然適用(只不過(guò)編譯時(shí)會(huì)有警告 'Human' may not respond to '-canCook:') Human *wife2 = [Woman new]; [wife2 canCook:@"soap"]; ? ? ? NSLog(@"----------------"); ? [pool drain]; return 0; } |
運(yùn)行結(jié)果:
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] —————-
轉(zhuǎn)載于:https://www.cnblogs.com/lm3515/archive/2011/04/08/2009825.html
總結(jié)
以上是生活随笔為你收集整理的Objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(property)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 转:基于Spark的电影推荐系统(包含爬
- 下一篇: eclipse 快捷调整字体_eclip