「OC」 多态

 

一、基本概念

  多态在代码中的体现,即为某一类事物的多种形态,OC对象具有多态性。必须要有继承,没有继承就没有多态。

  在使用多态时,会进行动态检测,以调用真实的对象方法。

  多态在代码中的体现即父类指针指向子类对象。

1 Person *p = [Student new];
2 
3 p->age = 100;
4 
5 [p walk];

二、多态使用注意

  1.多态的好处

    用父类接收参数,节省代码 

    需要一个新的函数专门用来喂狗

    void feed(Dog *d)

    {

      [d  eat];

    }

    如果这个时候也需要喂猫,那就应该重写新一个新的函数

    void feed(Cat *c)

    {

      [c  eat];

    }

    而狗和猫实际上都继承自动物这个类,在这里就可以使用多态来简化代码了。

    这里只需要把函数的参数写成是Animal *类型的,那么Dog和Cat类型的对象就都可以传入进来。

    调用的时候直接改变参数就可以了。

  2.多态的局限性

     不能访问子类的属性(可以考虑强制转换)

    不建议的做法:

    Animal *a=[[Dog alloc] init];

    [a run]; // 在Animal类中没有run方法,这里调用了狗对象的方法。

    解决方法:可以将a强制转换为Dog*类型的变量,如下:

    Dog *d=(Dog *)a; // 使用强制转换,这里a和d指向的是同一个狗对象

  3.多态的细节

    动态绑定:在运行时根据对象的类型确定动态调用的方法

三、NSString的简单使用

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 {
 5     //char *_name;
 6     NSString *_name;
 7 }
 8 @end
 9 
10 int main()
11 {
12     /*
13     // 最简单的创建字符串的方式
14     NSString *str = @"itcast";
15     
16     char *name = "itcast";
17     
18     
19     NSLog(@"我在%@上课", str);
20     //NSLog(@"%s", name);
21     */
22     
23     int age = 15;
24     int no = 5;
25 
26     NSString *name = @"哈哈jack";
27 
28     // length方法算的是字数
29     int size = [name length];
30     
31     NSLog(@"%d", size);
32     
33     // 创建OC字符串的另一种方式
34     NSString *newStr = [NSString stringWithFormat:@"My age is %d and no is %d and name is %@", age, no, name];
35     
36     
37     NSLog(@"---- %ld", [newStr length]);
38     
39     return 0;
40 }

  注意:

    字符串对象的length方法:计算的是字符串的字数,而不是像strlen函数那样,计算的是字符数。如“哈ha123”length得出的结果是6,返回unsigned long类型,而strlen函数得出的结果是8,因为一个汉字占3个字节。

  提示:字数也包括空格。

四、多态使用实例

 1 #import <Foundation/Foundation.h>
 2 
 3 // 动物
 4 @interface Animal : NSObject
 5 - (void)eat;
 6 @end
 7 
 8 @implementation Animal
 9 - (void)eat
10 {
11     NSLog(@"Animal-吃东西----");
12 }
13 @end
14 
15 //
16 @interface Dog : Animal
17 - (void)run;
18 @end
19 
20 @implementation  Dog
21 - (void)run
22 {
23     NSLog(@"Dog---跑起来");
24 }
25 - (void)eat
26 {
27     NSLog(@"Dog-吃东西----");
28 }
29 @end
30 
31 //
32 @interface Cat : Animal
33 
34 @end
35 
36 @implementation Cat
37 - (void)eat
38 {
39     NSLog(@"Cat-吃东西----");
40 }
41 @end
42 
43 // 如果参数中使用的是父类类型,可以传入父类、子类对象
44 void feed(Animal *a)
45 {
46     [a eat];
47 }
48 
49 int main()
50 {
51      NSString *d = [Cat new];
52      [d eat];
53     
54     /*
55     Animal *aa = [Dog new];
56     // 多态的局限性:父类类型的变量 不能 用来调用子类的方法
57     //[aa run];
58     
59     // 将aa转为Dog *类型的变量
60     Dog *dd = (Dog *)aa;
61     
62     [dd run];
63     */
64     
65     //Dog *d = [Dog new];
66     
67     //[d run];
68     
69     /*
70     Animal *aa = [Animal new];
71     feed(aa);
72     
73     Dog *dd = [Dog new];
74     feed(dd);
75     
76     Cat *cc = [Cat new];
77     feed(cc);
78      */
79     
80     /*
81     // NSString *s = [Cat new];
82     Animal *c = [Cat new];
83     
84     
85     NSObject *n = [Dog new];
86     NSObject *n2 = [Animal new];
87     
88     
89     // 多种形态
90     //Dog *d = [Dog new]; // Dog类型
91     
92     // 多态:父类指针指向子类对象
93     Animal *a = [Dog new];
94     
95     // 调用方法时会检测对象的真实形象
96     [a eat];
97     */
98     return 0;
99 }
原文地址:https://www.cnblogs.com/xiaodong208/p/4253014.html