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

OC 多态

时间:2015-01-25 22:30:37      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

  1 #import <Foundation/Foundation.h>
  2 
  3 /*
  4  多态
  5  1.没有继承就没有多态
  6  2.代码的体现:父类类型的指针指向子类对象
  7  3.好处:如果函数\方法参数中使用的是父类类型,可以传入父类、子类对象
  8  4.局限性:
  9  1> 父类类型的变量 不能 直接调用子类特有的方法。必须强转为子类类型变量后,才能直接调用子类特有的方法
 10  */
 11 
 12 // 动物
 13 @interface Animal : NSObject
 14 - (void)eat;
 15 @end
 16 
 17 @implementation Animal
 18 - (void)eat
 19 {
 20     NSLog(@"Animal-吃东西----");
 21 }
 22 @end
 23 
 24 /************************************************************************/
 25 /*                                                                      */
 26 /************************************************************************/
 27 
 28 //
 29 @interface Dog : Animal
 30 - (void)run;
 31 @end
 32 
 33 @implementation  Dog
 34 - (void)run
 35 {
 36     NSLog(@"Dog---跑起来");
 37 }
 38 - (void)eat
 39 {
 40     NSLog(@"Dog-吃东西----");
 41 }
 42 @end
 43 
 44 /************************************************************************/
 45 /*                                                                      */
 46 /************************************************************************/
 47 
 48 //
 49 @interface Cat : Animal
 50 
 51 @end
 52 
 53 @implementation Cat
 54 - (void)eat
 55 {
 56     NSLog(@"Cat-吃东西----");
 57 }
 58 @end
 59 
 60 /************************************************************************/
 61 /*                                                                      */
 62 /************************************************************************/
 63 
 64 // 如果参数中使用的是父类类型,可以传入父类、子类对象
 65 void feed(Animal *a)
 66 {
 67     [a eat];
 68 }
 69 
 70 /************************************************************************/
 71 /*                                                                      */
 72 /************************************************************************/
 73 
 74 int main()
 75 {
 76 
 77 /************************************************************************/
 78 
 79     Animal *aa = [Dog new];
 80     // 多态的局限性:父类类型的变量 不能 用来调用子类的方法
 81     //[aa run];
 82     
 83     // 将aa转为Dog *类型的变量
 84     Dog *dd = (Dog *)aa;
 85     
 86     [dd run];
 87 
 88 /************************************************************************/
 89     
 90     Dog *d = [Dog new];
 91     
 92     [d run];
 93 
 94 /************************************************************************/
 95     
 96     Animal *aa = [Animal new];
 97     feed(aa);
 98     
 99     Dog *dd = [Dog new];
100     feed(dd);
101     
102     Cat *cc = [Cat new];
103     feed(cc);
104 
105  /************************************************************************/
106 
107 
108     Animal *c = [Cat new];
109     
110     
111     // 多种形态
112     Dog *d = [Dog new]; // Dog类型
113     
114     // 多态:父类指针指向子类对象
115     Animal *a = [Dog new];
116     
117     // 调用方法时会检测对象的真实形象
118     [a eat];
119 
120     return 0;
121 }

 

OC 多态

标签:

原文地址:http://www.cnblogs.com/w413133157/p/4248975.html

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