码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-C代码学习大纲(4)

时间:2015-04-22 13:20:11      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

2011-05-11 14:06 佚名 otierney 字号:T | T
技术分享

本文为台湾出版的《Objective-C学习大纲》的翻译文档,系统介绍了Objective-C代码,很多名词为台湾同胞特指词汇,在学习时仔细研读才能体会。

AD:干货来了,不要等!WOT2015 北京站演讲PPT开放下载!

 

继承、多型(Inheritance, Polymorphism)以及其他物件导向功能

id 型别

Objective-C 有种叫做 id 的型别,它的运作有时候像是 void*,不过它却严格规定只能用在物件。Objective-C 与 Java 跟 C++ 不一样,你在唿叫一个物件的 method 时,并不需要知道这个物件的型别。当然这个 method 一定要存在,这称为 Objective-C 的讯息传递。

 

  1. Fraction.h 
  2.  
  3. #import 
  4.  
  5. @interface Fraction: NSObject { 
  6.  
  7. int numerator; 
  8.  
  9. int denominator; 
  10.  
  11.  
  12. -(Fraction*) initWithNumerator: (int) n denominator: (int) d; 
  13.  
  14. -(void) print; 
  15.  
  16. -(void) setNumerator: (int) d; 
  17.  
  18. -(void) setDenominator: (int) d; 
  19.  
  20. -(void) setNumerator: (int) n andDenominator: (int) d; 
  21.  
  22. -(int) numerator; 
  23.  
  24. -(int) denominator; 
  25.  
  26. @end 
  27.  
  28. Fraction.m 
  29.  
  30. #import "Fraction.h" 
  31.  
  32. #import 
  33.  
  34. @implementation Fraction 
  35.  
  36. -(Fraction*) initWithNumerator: (int) n denominator: (int) d { 
  37.  
  38. self = [super init]; 
  39.  
  40. if ( self ) { 
  41.  
  42. [self setNumerator: n andDenominator: d]; 
  43.  
  44.  
  45. return self; 
  46.  
  47.  
  48. -(void) print { 
  49.  
  50. printf( "%i / %i", numerator, denominator ); 
  51.  
  52.  
  53. -(void) setNumerator: (int) n { 
  54.  
  55. nnumerator = n; 
  56.  
  57.  
  58. -(void) setDenominator: (int) d { 
  59.  
  60. ddenominator = d; 
  61.  
  62.  
  63. -(void) setNumerator: (int) n andDenominator: (int) d { 
  64.  
  65. nnumerator = n; 
  66.  
  67. ddenominator = d; 
  68.  
  69.  
  70. -(int) denominator { 
  71.  
  72. return denominator; 
  73.  
  74.  
  75. -(int) numerator { 
  76.  
  77. return numerator; 
  78.  
  79.  
  80. @end 
  81.  
  82. Complex.h 
  83.  
  84. #import 
  85.  
  86. @interface Complex: NSObject { 
  87.  
  88. double real; 
  89.  
  90. double imaginary; 
  91.  
  92.  
  93. -(Complex*) initWithReal: (double) r andImaginary: (double) i; 
  94.  
  95. -(void) setReal: (double) r; 
  96.  
  97. -(void) setImaginary: (double) i; 
  98.  
  99. -(void) setReal: (double) r andImaginary: (double) i; 
  100.  
  101. -(double) real; 
  102.  
  103. -(double) imaginary; 
  104.  
  105. -(void) print; 
  106.  
  107. @end 
  108.  
  109. Complex.m 
  110.  
  111. #import "Complex.h" 
  112.  
  113. #import 
  114.  
  115. @implementation Complex 
  116.  
  117. -(Complex*) initWithReal: (double) r andImaginary: (double) i { 
  118.  
  119. self = [super init]; 
  120.  
  121. if ( self ) { 
  122.  
  123. [self setReal: r andImaginary: i]; 
  124.  
  125.  
  126. return self; 
  127.  
  128.  
  129. -(void) setReal: (double) r { 
  130.  
  131. rreal = r; 
  132.  
  133.  
  134. -(void) setImaginary: (double) i { 
  135.  
  136. iimaginary = i; 
  137.  
  138.  
  139. -(void) setReal: (double) r andImaginary: (double) i { 
  140.  
  141. rreal = r; 
  142.  
  143. iimaginary = i; 
  144.  
  145.  
  146. -(double) real { 
  147.  
  148. return real; 
  149.  
  150.  
  151. -(double) imaginary { 
  152.  
  153. return imaginary; 
  154.  
  155.  
  156. -(void) print { 
  157.  
  158. printf( "%_f + %_fi", real, imaginary ); 
  159.  
  160.  
  161. @end 
  162.  
  163. main.m 
  164.  
  165. #import 
  166.  
  167. #import "Fraction.h" 
  168.  
  169. #import "Complex.h" 
  170.  
  171. int main( int argc, const char *argv[] ) { 
  172.  
  173. // create a new instance 
  174.  
  175. Fraction *frac = [[Fraction alloc] initWithNumerator: 1 denominator: 10]; 
  176.  
  177. Complex *comp = [[Complex alloc] initWithReal: 10 andImaginary: 15]; 
  178.  
  179. id number; 
  180.  
  181. // print fraction 
  182.  
  183. number = frac; 
  184.  
  185. printf( "The fraction is: " ); 
  186.  
  187. [number print]; 
  188.  
  189. printf( "\n" ); 
  190.  
  191. // print complex 
  192.  
  193. number = comp; 
  194.  
  195. printf( "The complex number is: " ); 
  196.  
  197. [number print]; 
  198.  
  199. printf( "\n" ); 
  200.  
  201. // free memory 
  202.  
  203. [frac release]; 
  204.  
  205. [comp release]; 
  206.  
  207. return 0; 
  208.  

 

output

  1. The fraction is: 1 / 10 
  2.  
  3. The complex number is: 10.000000 + 15.000000i 

这种动态连结有显而易见的好处。你不需要知道你唿叫 method 的那个东西是什么型别,如果这个物件对这个讯息有反应,那就会唤起这个 method。这也不会牵涉到一堆繁琐的转型动作,比如在 Java 裡唿叫一个整数物件的 .intValue() 就得先转型,然后才能唿叫这个 method。

继承(Inheritance)

 

  1. Rectangle.h 
  2.  
  3. #import 
  4.  
  5. @interface Rectangle: NSObject { 
  6.  
  7. int width; 
  8.  
  9. int height; 
  10.  
  11.  
  12. -(Rectangle*) initWithWidth: (int) w height: (int) h; 
  13.  
  14. -(void) setWidth: (int) w; 
  15.  
  16. -(void) setHeight: (int) h; 
  17.  
  18. -(void) setWidth: (int) w height: (int) h; 
  19.  
  20. -(int) width; 
  21.  
  22. -(int) height; 
  23.  
  24. -(void) print; 
  25.  
  26. @end 
  27.  
  28. Rectangle.m 
  29.  
  30. #import "Rectangle.h" 
  31.  
  32. #import 
  33.  
  34. @implementation Rectangle 
  35.  
  36. -(Rectangle*) initWithWidth: (int) w height: (int) h { 
  37.  
  38. self = [super init]; 
  39.  
  40. if ( self ) { 
  41.  
  42. [self setWidth: w height: h]; 
  43.  
  44.  
  45. return self; 
  46.  
  47.  
  48. -(void) setWidth: (int) w { 
  49.  
  50. wwidth = w; 
  51.  
  52.  
  53. -(void) setHeight: (int) h { 
  54.  
  55. hheight = h; 
  56.  
  57.  
  58. -(void) setWidth: (int) w height: (int) h { 
  59.  
  60. wwidth = w; 
  61.  
  62. hheight = h; 
  63.  
  64.  
  65. -(int) width { 
  66.  
  67. return width; 
  68.  
  69.  
  70. -(int) height { 
  71.  
  72. return height; 
  73.  
  74.  
  75. -(void) print { 
  76.  
  77. printf( "width = %i, height = %i", width, height ); 
  78.  
  79.  
  80. @end 
  81.  
  82. Square.h 
  83.  
  84. #import "Rectangle.h" 
  85.  
  86. @interface Square: Rectangle 
  87.  
  88. -(Square*) initWithSize: (int) s; 
  89.  
  90. -(void) setSize: (int) s; 
  91.  
  92. -(int) size; 
  93.  
  94. @end 
  95.  
  96. Square.m 
  97.  
  98. #import "Square.h" 
  99.  
  100. @implementation Square 
  101.  
  102. -(Square*) initWithSize: (int) s { 
  103.  
  104. self = [super init]; 
  105.  
  106. if ( self ) { 
  107.  
  108. [self setSize: s]; 
  109.  
  110.  
  111. return self; 
  112.  
  113.  
  114. -(void) setSize: (int) s { 
  115.  
  116. width = s; 
  117.  
  118. height = s; 
  119.  
  120.  
  121. -(int) size { 
  122.  
  123. return width; 
  124.  
  125.  
  126. -(void) setWidth: (int) w { 
  127.  
  128. [self setSize: w]; 
  129.  
  130.  
  131. -(void) setHeight: (int) h { 
  132.  
  133. [self setSize: h]; 
  134.  
  135.  
  136. @end 
  137.  
  138. main.m 
  139.  
  140. #import "Square.h" 
  141.  
  142. #import "Rectangle.h" 
  143.  
  144. #import 
  145.  
  146. int main( int argc, const char *argv[] ) { 
  147.  
  148. Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20]; 
  149.  
  150. Square *sq = [[Square alloc] initWithSize: 15]; 
  151.  
  152. // print em 
  153.  
  154. printf( "Rectangle: " ); 
  155.  
  156. [rec print]; 
  157.  
  158. printf( "\n" ); 
  159.  
  160. printf( "Square: " ); 
  161.  
  162. [sq print]; 
  163.  
  164. printf( "\n" ); 
  165.  
  166. // update square 
  167.  
  168. [sq setWidth: 20]; 
  169.  
  170. printf( "Square after change: " ); 
  171.  
  172. [sq print]; 
  173.  
  174. printf( "\n" ); 
  175.  
  176. // free memory 
  177.  
  178. [rec release]; 
  179.  
  180. [sq release]; 
  181.  
  182. return 0; 
  183.  

 

output

  1. Rectangle: width = 10, height = 20 
  2.  
  3. Square: width = 15, height = 15 
  4.  
  5. Square after change: width = 20, height = 20 

继承在 Objective-C 裡比较像 Java。当你扩充你的 super class(所以只能有一个 parent),你想自订这个 super class 的 method,只要简单的在你的 child class implementation 裡放上新的实作内容即可。而不需要 C++ 裡呆呆的 virtual table。

这裡还有一个值得玩味的地方,如果你企图像这样去唿叫 rectangle 的 constructor: Square *sq = [[Square alloc] initWithWidth: 10 height: 15],会发生什么事?答案是会产生一个编译器错误。因为 rectangle constructor 回传的型别是 Rectangle*,而不是 Square*,所以这行不通。在某种情况下如果你真想这样用,使用 id 型别会是很好的选择。如果你想使用 parent 的 constructor,只要把 Rectangle* 回传型别改成 id 即可。

动态识别(Dynamic types)

这裡有一些用于 Objective-C 动态识别的 methods(说明部分採中英并列,因为我觉得英文比较传神,中文怎么译都怪):

  1. -(BOOL) isKindOfClass: classObjis object a descendent or member of classObj 

此物件是否是 classObj 的子孙或一员

  1. -(BOOL) isMemberOfClass: classObjis object a member of classObj 

此物件是否是 classObj 的一员

  1. -(BOOL) respondsToSelector: selectordoes the object have a method named specifiec by the selector 

此物件是否有叫做 selector 的 method

  1. +(BOOL) instancesRespondToSelector: selectordoes an object created by this class have the ability to respond to the specified selector 

此物件是否是由有能力回应指定 selector 的物件所产生

  1. -(id) performSelector: selectorinvoke the specified selector on the object 

唤起此物件的指定 selector

所有继承自 NSObject 都有一个可回传一个 class 物件的 class method。这非常近似于 Java 的 getClass() method。这个 class 物件被使用于前述的 methods 中。

Selectors 在 Objective-C 用以表示讯息。下一个範例会秀出建立 selector 的语法。

 

  1. main.m 
  2.  
  3. #import "Square.h" 
  4.  
  5. #import "Rectangle.h" 
  6.  
  7. #import 
  8.  
  9. int main( int argc, const char *argv[] ) { 
  10.  
  11. Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20]; 
  12.  
  13. Square *sq = [[Square alloc] initWithSize: 15]; 
  14.  
  15. // isMemberOfClass 
  16.  
  17. // true 
  18.  
  19. if ( [sq isMemberOfClass: [Square class]] == YES ) { 
  20.  
  21. printf( "square is a member of square class\n" ); 
  22.  
  23.  
  24. // false 
  25.  
  26. if ( [sq isMemberOfClass: [Rectangle class]] == YES ) { 
  27.  
  28. printf( "square is a member of rectangle class\n" ); 
  29.  
  30.  
  31. // false 
  32.  
  33. if ( [sq isMemberOfClass: [NSObject class]] == YES ) { 
  34.  
  35. printf( "square is a member of object class\n" ); 
  36.  
  37.  
  38. // isKindOfClass 
  39.  
  40. // true 
  41.  
  42. if ( [sq isKindOfClass: [Square class]] == YES ) { 
  43.  
  44. printf( "square is a kind of square class\n" ); 
  45.  
  46.  
  47. // true 
  48.  
  49. if ( [sq isKindOfClass: [Rectangle class]] == YES ) { 
  50.  
  51. printf( "square is a kind of rectangle class\n" ); 
  52.  
  53.  
  54. // true 
  55.  
  56. if ( [sq isKindOfClass: [NSObject class]] == YES ) { 
  57.  
  58. printf( "square is a kind of object class\n" ); 
  59.  
  60.  
  61. // respondsToSelector 
  62.  
  63. // true 
  64.  
  65. if ( [sq respondsToSelector: @selector( setSize: )] == YES ) { 
  66.  
  67. printf( "square responds to setSize: method\n" ); 
  68.  
  69.  
  70. // false 
  71.  
  72. if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) { 
  73.  
  74. printf( "square responds to nonExistant method\n" ); 
  75.  
  76.  
  77. // true 
  78.  
  79. if ( [Square respondsToSelector: @selector( alloc )] == YES ) { 
  80.  
  81. printf( "square class responds to alloc method\n" ); 
  82.  
  83.  
  84. // instancesRespondToSelector 
  85.  
  86. // false 
  87.  
  88. if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) { 
  89.  
  90. printf( "rectangle instance responds to setSize: method\n" ); 
  91.  
  92.  
  93. // true 
  94.  
  95. if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) { 
  96.  
  97. printf( "square instance responds to setSize: method\n" ); 
  98.  
  99.  
  100. // free memory 
  101.  
  102. [rec release]; 
  103.  
  104. [sq release]; 
  105.  
  106. return 0; 
  107.  

 

output

  1. square is a member of square class 
  2.  
  3. square is a kind of square class 
  4.  
  5. square is a kind of rectangle class 
  6.  
  7. square is a kind of object class 
  8.  
  9. square responds to setSize: method 
  10.  
  11. square class responds to alloc method 
  12.  
  13. square instance responds to setSize: method 
  14.  
  15. Categories 

当你想要为某个 class 新增 methods,你通常会扩充(extend,即继承)它。然而这不一定是个完美解法,特别是你想要重写一个 class 的某个功能,但你却没有塬始码时。Categories 允许你在现有的 class 加入新功能,但不需要扩充它。Ruby 语言也有类似的功能。

 

  1. FractionMath.h 
  2.  
  3. #import "Fraction.h" 
  4.  
  5. @interface Fraction (Math) 
  6.  
  7. -(Fraction*) add: (Fraction*) f; 
  8.  
  9. -(Fraction*) mul: (Fraction*) f; 
  10.  
  11. -(Fraction*) div: (Fraction*) f; 
  12.  
  13. -(Fraction*) sub: (Fraction*) f; 
  14.  
  15. @end 
  16.  
  17. FractionMath.m 
  18.  
  19. #import "FractionMath.h" 
  20.  
  21. @implementation Fraction (Math) 
  22.  
  23. -(Fraction*) add: (Fraction*) f { 
  24.  
  25. return [[Fraction alloc] initWithNumerator: numerator * [f denominator] + 
  26.  
  27. denominator * [f numerator] 
  28.  
  29. denominator: denominator * [f denominator]]; 
  30.  
  31.  
  32. -(Fraction*) mul: (Fraction*) f { 
  33.  
  34. return [[Fraction alloc] initWithNumerator: numerator * [f numerator] 
  35.  
  36. denominator: denominator * [f denominator]]; 
  37.  
  38.  
  39. -(Fraction*) div: (Fraction*) f { 
  40.  
  41. return [[Fraction alloc] initWithNumerator: numerator * [f denominator] 
  42.  
  43. denominator: denominator * [f numerator]]; 
  44.  
  45.  
  46. -(Fraction*) sub: (Fraction*) f { 
  47.  
  48. return [[Fraction alloc] initWithNumerator: numerator * [f denominator] - 
  49.  
  50. denominator * [f numerator] 
  51.  
  52. denominator: denominator * [f denominator]]; 
  53.  
  54.  
  55. @end 
  56.  
  57. main.m 
  58.  
  59. #import 
  60.  
  61. #import "Fraction.h" 
  62.  
  63. #import "FractionMath.h" 
  64.  
  65. int main( int argc, const char *argv[] ) { 
  66.  
  67. // create a new instance 
  68.  
  69. Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3]; 
  70.  
  71. Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5]; 
  72.  
  73. Fraction *frac3 = [frac1 mul: frac2]; 
  74.  
  75. // print it 
  76.  
  77. [frac1 print]; 
  78.  
  79. printf( " * " ); 
  80.  
  81. [frac2 print]; 
  82.  
  83. printf( " = " ); 
  84.  
  85. [frac3 print]; 
  86.  
  87. printf( "\n" ); 
  88.  
  89. // free memory 
  90.  
  91. [frac1 release]; 
  92.  
  93. [frac2 release]; 
  94.  
  95. [frac3 release]; 
  96.  
  97. return 0; 
  98.  

 

output

  1. 1/3 * 2/5 = 2/15 

重点是 @implementation 跟 @interface 这两行:@interface Fraction (Math) 以及 @implementation Fraction (Math).

(同一个 class)只能有一个同名的 category,其他的 categories 得加上不同的、独一无二的名字。

Categories 在建立 private methods 时十分有用。因为 Objective-C 并没有像 Java 这种 private/protected/public methods 的概念,所以必须要使用 categories 来达成这种功能。作法是把 private method 从你的 class header (.h) 档案移到 implementation (.m) 档案。以下是此种作法一个简短的範例。

  1. MyClass.h 
  2.  
  3. #import 
  4.  
  5. @interface MyClass: NSObject 
  6.  
  7. -(void) publicMethod; 
  8.  
  9. @end 
  10.  
  11. MyClass.m 
  12.  
  13. #import "MyClass.h" 
  14.  
  15. #import 
  16.  
  17. @implementation MyClass 
  18.  
  19. -(void) publicMethod { 
  20.  
  21. printf( "public method\n" ); 
  22.  
  23.  
  24. @end 
  25.  
  26. // private methods 
  27.  
  28. @interface MyClass (Private) 
  29.  
  30. -(void) privateMethod; 
  31.  
  32. @end 
  33.  
  34. @implementation MyClass (Private) 
  35.  
  36. -(void) privateMethod { 
  37.  
  38. printf( "private method\n" ); 
  39.  
  40.  
  41. @end 
  42.  
  43. main.m 
  44.  
  45. #import "MyClass.h" 
  46.  
  47. int main( int argc, const char *argv[] ) { 
  48.  
  49. MyClass *obj = [[MyClass alloc] init]; 
  50.  
  51. // this compiles 
  52.  
  53. [obj publicMethod]; 
  54.  
  55. // this throws errors when compiling 
  56.  
  57. //[obj privateMethod]; 
  58.  
  59. // free memory 
  60.  
  61. [obj release]; 
  62.  
  63. return 0; 
  64.  

output

  1. public method 
  2.  
  3. Posing 

Posing 有点像 categories,但是不太一样。它允许你扩充一个 class,并且全面性地的扮演(pose)这个 super class。例如:你有一个扩充 NSArray 的 NSArrayChild 物件。如果你让 NSArrayChild 扮演 NSArray,则在你的程式码中所有的 NSArray 都会自动被替代为 NSArrayChild。

  1. FractionB.h 
  2.  
  3. #import "Fraction.h" 
  4.  
  5. @interface FractionB: Fraction 
  6.  
  7. -(void) print; 
  8.  
  9. @end 
  10.  
  11. FractionB.m 
  12.  
  13. #import "FractionB.h" 
  14.  
  15. #import 
  16.  
  17. @implementation FractionB 
  18.  
  19. -(void) print { 
  20.  
  21. printf( "(%i/%i)", numerator, denominator ); 
  22.  
  23.  
  24. @end 
  25.  
  26. main.m 
  27.  
  28. #import 
  29.  
  30. #import "Fraction.h" 
  31.  
  32. #import "FractionB.h" 
  33.  
  34. int main( int argc, const char *argv[] ) { 
  35.  
  36. Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; 
  37.  
  38. // print it 
  39.  
  40. printf( "The fraction is: " ); 
  41.  
  42. [frac print]; 
  43.  
  44. printf( "\n" ); 
  45.  
  46. // make FractionB pose as Fraction 
  47.  
  48. [FractionB poseAsClass: [Fraction class]]; 
  49.  
  50. Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; 
  51.  
  52. // print it 
  53.  
  54. printf( "The fraction is: " ); 
  55.  
  56. [frac2 print]; 
  57.  
  58. printf( "\n" ); 
  59.  
  60. // free memory 
  61.  
  62. [frac release]; 
  63.  
  64. [frac2 release]; 
  65.  
  66. return 0; 
  67.  

 

output

  1. The fraction is: 3/10 
  2.  
  3. The fraction is: (3/10) 

这个程式的输出中,第一个 fraction 会输出 3/10,而第二个会输出 (3/10)。这是 FractionB 中实作的方式。

poseAsClass 这个 method 是 NSObject 的一部份,它允许 subclass 扮演 superclass。

Protocols

Objective-C 裡的 Protocol 与 Java 的 interface 或是 C++ 的 purely virtual class 相同。

 

  1. Printing.h 
  2.  
  3. @protocol Printing 
  4.  
  5. -(void) print; 
  6.  
  7. @end 
  8.  
  9. Fraction.h 
  10.  
  11. #import 
  12.  
  13. #import "Printing.h" 
  14.  
  15. @interface Fraction: NSObject { 
  16.  
  17. int numerator; 
  18.  
  19. int denominator; 
  20.  
  21.  
  22. -(Fraction*) initWithNumerator: (int) n denominator: (int) d; 
  23.  
  24. -(void) setNumerator: (int) d; 
  25.  
  26. -(void) setDenominator: (int) d; 
  27.  
  28. -(void) setNumerator: (int) n andDenominator: (int) d; 
  29.  
  30. -(int) numerator; 
  31.  
  32. -(int) denominator; 
  33.  
  34. @end 
  35.  
  36. Fraction.m 
  37.  
  38. #import "Fraction.h" 
  39.  
  40. #import 
  41.  
  42. @implementation Fraction 
  43.  
  44. -(Fraction*) initWithNumerator: (int) n denominator: (int) d { 
  45.  
  46. self = [super init]; 
  47.  
  48. if ( self ) { 
  49.  
  50. [self setNumerator: n andDenominator: d]; 
  51.  
  52.  
  53. return self; 
  54.  
  55.  
  56. -(void) print { 
  57.  
  58. printf( "%i/%i", numerator, denominator ); 
  59.  
  60.  
  61. -(void) setNumerator: (int) n { 
  62.  
  63. nnumerator = n; 
  64.  
  65.  
  66. -(void) setDenominator: (int) d { 
  67.  
  68. ddenominator = d; 
  69.  
  70.  
  71. -(void) setNumerator: (int) n andDenominator: (int) d { 
  72.  
  73. nnumerator = n; 
  74.  
  75. ddenominator = d; 
  76.  
  77.  
  78. -(int) denominator { 
  79.  
  80. return denominator; 
  81.  
  82.  
  83. -(int) numerator { 
  84.  
  85. return numerator; 
  86.  
  87.  
  88. -(Fraction*) copyWithZone: (NSZone*) zone { 
  89.  
  90. return [[Fraction allocWithZone: zone] initWithNumerator: numerator 
  91.  
  92. denominator: denominator]; 
  93.  
  94.  
  95. @end 
  96.  
  97. Complex.h 
  98.  
  99. #import 
  100.  
  101. #import "Printing.h" 
  102.  
  103. @interface Complex: NSObject { 
  104.  
  105. double real; 
  106.  
  107. double imaginary; 
  108.  
  109.  
  110. -(Complex*) initWithReal: (double) r andImaginary: (double) i; 
  111.  
  112. -(void) setReal: (double) r; 
  113.  
  114. -(void) setImaginary: (double) i; 
  115.  
  116. -(void) setReal: (double) r andImaginary: (double) i; 
  117.  
  118. -(double) real; 
  119.  
  120. -(double) imaginary; 
  121.  
  122. @end 
  123.  
  124. Complex.m 
  125.  
  126. #import "Complex.h" 
  127.  
  128. #import 
  129.  
  130. @implementation Complex 
  131.  
  132. -(Complex*) initWithReal: (double) r andImaginary: (double) i { 
  133.  
  134. self = [super init]; 
  135.  
  136. if ( self ) { 
  137.  
  138. [self setReal: r andImaginary: i]; 
  139.  
  140.  
  141. return self; 
  142.  
  143.  
  144. -(void) setReal: (double) r { 
  145.  
  146. rreal = r; 
  147.  
  148.  
  149. -(void) setImaginary: (double) i { 
  150.  
  151. iimaginary = i; 
  152.  
  153.  
  154. -(void) setReal: (double) r andImaginary: (double) i { 
  155.  
  156. rreal = r; 
  157.  
  158. iimaginary = i; 
  159.  
  160.  
  161. -(double) real { 
  162.  
  163. return real; 
  164.  
  165.  
  166. -(double) imaginary { 
  167.  
  168. return imaginary; 
  169.  
  170.  
  171. -(void) print { 
  172.  
  173. printf( "%_f + %_fi", real, imaginary ); 
  174.  
  175.  
  176. @end 
  177.  
  178. main.m 
  179.  
  180. #import 
  181.  
  182. #import "Fraction.h" 
  183.  
  184. #import "Complex.h" 
  185.  
  186. int main( int argc, const char *argv[] ) { 
  187.  
  188. // create a new instance 
  189.  
  190. Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; 
  191.  
  192. Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15]; 
  193.  
  194. id printable; 
  195.  
  196. id copyPrintable; 
  197.  
  198. // print it 
  199.  
  200. printable = frac; 
  201.  
  202. printf( "The fraction is: " ); 
  203.  
  204. [printable print]; 
  205.  
  206. printf( "\n" ); 
  207.  
  208. // print complex 
  209.  
  210. printable = comp; 
  211.  
  212. printf( "The complex number is: " ); 
  213.  
  214. [printable print]; 
  215.  
  216. printf( "\n" ); 
  217.  
  218. // this compiles because Fraction comforms to both Printing and NSCopyable 
  219.  
  220. copyPrintable = frac; 
  221.  
  222. // this doesn‘t compile because Complex only conforms to Printing 
  223.  
  224. //copyPrintable = comp; 
  225.  
  226. // test conformance 
  227.  
  228. // true 
  229.  
  230. if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) { 
  231.  
  232. printf( "Fraction conforms to NSCopying\n" ); 
  233.  
  234.  
  235. // false 
  236.  
  237. if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) { 
  238.  
  239. printf( "Complex conforms to NSCopying\n" ); 
  240.  
  241.  
  242. // free memory 
  243.  
  244. [frac release]; 
  245.  
  246. [comp release]; 
  247.  
  248. return 0; 
  249.  

 

output

  1. The fraction is: 3/10 
  2.  
  3. The complex number is: 5.000000 + 15.000000i 
  4.  
  5. Fraction conforms to NSCopying 

protocol 的宣告十分简单,基本上就是 @protocol ProtocolName (methods you must implement) @end。

要遵从(conform)某个 protocol,将要遵从的 protocols 放在 <> 裡面,并以逗点分隔。如:@interface SomeClass

protocol 要求实作的 methods 不需要放在 header 档裡面的 methods 列表中。如你所见,Complex.h 档案裡没有 -(void) print 的宣告,却还是要实作它,因为它(Complex class)遵从了这个 protocol。

Objective-C 的介面系统有一个独一无二的观念是如何指定一个型别。比起 C++ 或 Java 的指定方式,如:Printing *someVar = ( Printing * ) frac; 你可以使用 id 型别加上 protocol:id var = frac;。这让你可以动态地指定一个要求多个 protocol 的型别,却从头到尾只用了一个变数。如: var = frac;

就像使用@selector 来测试物件的继承关係,你可以使用 @protocol 来测试物件是否遵从介面。如果物件遵从这个介面,[object conformsToProtocol: @protocol( SomeProtocol )] 会回传一个 YES 型态的 BOOL 物件。同样地,对 class 而言也能如法炮製 [SomeClass conformsToProtocol: @protocol( SomeProtocol )]。

Objective-C代码学习大纲(4)

标签:

原文地址:http://www.cnblogs.com/lihaibo-Leao/p/4447042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!