多态、动态类型和动态绑定

作者:追风剑情 发布于:2019-2-22 14:16 分类:Objective-C

      多态能够使来自不同类的对象定义相同的方法。动态类型能使程序直到执行时才确定对象所属的类。动态绑定则能使程序直到执行时才确定实际要调用的对象的方法。

示例一:多态

Fraction.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Fraction : NSObject
  4.  
  5. //@property指令让编译器自动为numerator、denominator生成getter、setter方法
  6. @property int numerator, denominator;
  7.  
  8. -(void) print;
  9. +(int) printCallCount;
  10. -(double) convertToNum;
  11. -(void) setTo: (int) n over: (int) d;
  12. -(void) set: (int) n : (int) d;
  13. -(Fraction *) add: (Fraction *) f;
  14. -(void) reduce;
  15.  
  16. @end


Fraction.m

  1. #import "Fraction.h"
  2.  
  3. @implementation Fraction
  4.  
  5. @synthesize numerator, denominator;
  6.  
  7. // 统计所有对象调用print方法的次数,默认值为0
  8. static int printCount;
  9.  
  10. // 静太方法
  11. +(int) printCallCount
  12. {
  13. return printCount;
  14. }
  15.  
  16. -(void) print
  17. {
  18. printCount++;
  19. //统计本实例对象调用print方法的次数,局部静态变量只会在方法第一次调用时初始化一次
  20. static int printCountIns = 0;
  21. printCountIns++;
  22. NSLog(@"%i/%i", numerator, denominator);
  23. }
  24.  
  25. -(double) convertToNum
  26. {
  27. if (denominator != 0)
  28. return (double) numerator / denominator;
  29. else
  30. return NAN;
  31. }
  32.  
  33. // 多个参数的方法
  34. -(void) setTo: (int) n over: (int) d
  35. {
  36. numerator = n;
  37. denominator = d;
  38. }
  39.  
  40. // 省略参数名的多个参数方法
  41. // 注意,第一个参数名不能省
  42. // 省略参数名不是一种好的编程风格,因为它使程序很难读懂并且很不直观,特别是参数很重要时。
  43. -(void) set: (int) n : (int) d
  44. {
  45. numerator = n;
  46. denominator = d;
  47. }
  48.  
  49. // 分数相加
  50. -(Fraction *) add: (Fraction *) f
  51. {
  52. // 添加两个分数
  53. // a/b+c/d=((a*d)+(b*c))/(b*d)
  54. // 创建一个新对象来存储结果
  55. Fraction *result = [[Fraction alloc] init];
  56. result.numerator = numerator * f.denominator + denominator * f.numerator;
  57. result.denominator = denominator * f.denominator;
  58. // self关键字相当于C#的this
  59. // [self reduce];
  60. [result reduce];
  61. return result;
  62. }
  63.  
  64. // 约分
  65. -(void) reduce
  66. {
  67. int u = numerator;
  68. int v = denominator;
  69. int temp;
  70. while (v != 0) {
  71. temp = u % v;
  72. u = v;
  73. v = temp;
  74. }
  75. numerator /= u;
  76. denominator /= u;
  77. }
  78.  
  79. @end


Complex.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Complex : NSObject
  4.  
  5. @property double real, imaginary;
  6. -(void) print;
  7. -(void) setReal: (double) a andImaginary: (double) b;
  8. -(Complex *) add: (Complex *) f;
  9.  
  10. @end


Complex.m

  1. #import "Complex.h"
  2.  
  3. @implementation Complex
  4.  
  5. @synthesize real, imaginary;
  6.  
  7. -(void) print
  8. {
  9. NSLog(@" %g + %gi ", real, imaginary);
  10. }
  11.  
  12. -(void) setReal: (double) a andImaginary: (double) b
  13. {
  14. real = a;
  15. imaginary = b;
  16. }
  17.  
  18. -(Complex *) add: (Complex *) f
  19. {
  20. Complex *result = [[Complex alloc] init];
  21. result.real = real + f.real;
  22. result.imaginary = imaginary + f.imaginary;
  23. return result;
  24. }
  25.  
  26. @end


main.m

  1. #import <Foundation/Foundation.h>
  2. #import "Fraction.h"
  3. #import "Complex.h"
  4.  
  5. int main(int argc, const char * argv[]) {
  6. @autoreleasepool {
  7. Fraction *f1 = [[Fraction alloc] init];
  8. Fraction *f2 = [[Fraction alloc] init];
  9. Fraction *fracResult;
  10. Complex *c1 = [[Complex alloc] init];
  11. Complex *c2 = [[Complex alloc] init];
  12. Complex *compResult;
  13. [f1 setTo: 1 over: 10];
  14. [f2 setTo: 2 over: 15];
  15. [c1 setReal: 18.0 andImaginary: 2.5];
  16. [c2 setReal: -5.0 andImaginary: 3.2];
  17. // 将两个复数加相并显示
  18. [c1 print]; NSLog(@" +"); [c2 print];
  19. NSLog(@"----------");
  20. compResult = [c1 add: c2];
  21. [compResult print];
  22. NSLog(@"\n");
  23. // 将两个分数相加并显示
  24. [f1 print]; NSLog(@" +"); [f2 print];
  25. NSLog(@"----");
  26. fracResult = [f1 add: f2];
  27. [fracResult print];
  28. // Fraction和Complex都有add和print方法
  29. // 使不同的类共享相同方法名称的能力称为多态
  30. }
  31. return 0;
  32. }

运行测试
3333.png

示例二:动态绑定和id类型

main.m

  1. #import <Foundation/Foundation.h>
  2. #import "Fraction.h"
  3. #import "Complex.h"
  4.  
  5. int main(int argc, const char * argv[]) {
  6. @autoreleasepool {
  7. id dataValue;
  8. Fraction *f1 = [[Fraction alloc] init];
  9. Complex *c1 = [[Complex alloc] init];
  10.  
  11. [f1 setTo: 2 over: 5];
  12. [c1 setReal: 10.0 andImaginary: 2.5];
  13. // 动态绑定
  14. // 存储在id变量中的对象类型在编译时无法确定,一些测试推迟到运行时进行
  15. // id变量不能使用点运算符
  16. dataValue = f1;
  17. [dataValue print];
  18. dataValue = c1;
  19. [dataValue print];
  20. }
  21. return 0;
  22. }

运行测试

4444.png

处理动态类型的方法
方法 问题或行为
-(BOOL) isKindOfClass: class-object 对象是不是class-object或其子类的成员
-(BOOL) isMemberOfclass: class-object 对象是不是class-object的成员
-(BOOL) respondsToSelector: Selector 对象是否能够响应selector所指定的方法
+(BOOL) instancesRespondToSelector: Selector 指定的类实例是否能响应selector
+(BOOL) isSubclassOfClass: class-object 对象是否是指定类的子类
-(id) performSelector: selector 应用selector指定的方法
-(id) performSelector: selector withObject: object 应用selector指定的方法,传递参数object
-(id) performSelector: selector withObject: object1 withObject2 object2 应用selector指定的方法,传递参数object1和object2

示例三
用到了之前文章中的Rectangle类、Square类
参见 http://www.devacg.com/?post=924

main.m

  1. #import <Foundation/Foundation.h>
  2. #import "Square.h"
  3. #import "Rectangle.h"
  4.  
  5. int main(int argc, const char * argv[]) {
  6. @autoreleasepool {
  7. Square *mySquare = [[Square alloc] init];
  8. // isMemberOf: 测试类中的直接成员关系
  9. if ( [mySquare isMemberOfClass: [Square class]] == YES )
  10. NSLog(@"mySquare is a member of Square class");
  11. if ( [mySquare isMemberOfClass: [Rectangle class]] == YES )
  12. NSLog(@"mySquare is a member of Rectangle class");
  13. if ( [mySquare isMemberOfClass: [NSObject class]] == YES )
  14. NSLog(@"mySquare is a member of NSObject class");
  15. // isKindOf: 检测继承层次中的关系
  16. if ( [mySquare isKindOfClass: [Square class]] == YES )
  17. NSLog(@"mySquare is a kind of Square");
  18. if ( [mySquare isKindOfClass: [Rectangle class]] == YES )
  19. NSLog(@"mySquare is a kind of Rectangle");
  20. if ( [mySquare isKindOfClass: [NSObject class]] == YES )
  21. NSLog(@"mySquare is a kind of NSObject");
  22. // respondsTo:
  23. if ( [mySquare respondsToSelector: @selector (setSide:)] == YES )
  24. NSLog(@"mySquare responds to setSide: method");
  25. if ( [mySquare respondsToSelector: @selector (setWidth:andHeight:)] == YES )
  26. NSLog(@"mySquare responds to setWidth:andHeight: method");
  27. if ( [Square respondsToSelector: @selector (alloc)] == YES )
  28. NSLog(@"Square responds to alloc method");
  29. // instancesRespondTo:
  30. if ( [Rectangle instancesRespondToSelector: @selector (setSide:)] == YES )
  31. NSLog(@"Instances of Rectangle respond to setSide: method");
  32. if ( [Square instancesRespondToSelector: @selector (setSide:)] == YES )
  33. NSLog(@"Instances of Square respond to setSide: method");
  34. if ( [Square isSubclassOfClass: [Rectangle class]] == YES )
  35. NSLog(@"Square is a subclass of a rectangle");
  36. }
  37. return 0;
  38. }

运行测试
55555.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号