5 面向对象的三大特性

 
面向对象的三大特性是什么? 封装、继承、多态
封装:
  • 在OC语言中,使用@interface和@implementation来处理类。
@interface就好像暴露在外面的时钟表面,给外界提供展示以及接口。@implementation就好像隐藏在时钟内部的构造实现,把具体的实现封装了起来。
 
封装主要体现在:实例变量、对象方法、类方法的封装
 
  • set方法
在开发过程中,考虑到安全性要求,我们一般不在成员变量名前面使用@public、@protected等关键字修饰
,而是使用Set方法来为对象提供成员变量的值。在set方法的内部也可以对一些不合理的赋值进行筛选滤。
Set方法的作用:为外界提供一个设置成员变量值的方法
命名规范
(1)方法名必须以set开头
(2)set后面跟上成员变量的名称,首字母大写
(3)返回值一定是void
(4)一定要接收一个参数,而且参数类型需要和成员变量的类型一致
(5)形参名不能跟成员变量名一样(苹果官方推荐成员变量名前加_以示区分)
Set方法的好处:
(1)不让数据暴露在外,保证了数据的安全性
(2)对设置的数据进行过滤
Set方法使用示例:
Set方法的声明:
Set方法的实现:
测试程序:
  • get方法
  • Get方法的作用:为调用者返回对象内部的成员变量
  • 命名规范:
  • (1)一定有返回值,返回值的类型和成员变量的类型一致
  • (2)方法名和成员变量名一样
  • (3)不需要接收任何参数
  • Get方法使用示例:
  • Get方法的声明:
 
  • Get方法实现
 
  • 测试程序
 
注意1:在实际的开发中,不一定set和get方法都会提供,如果内部的成员变量比如学生的学号这样的数据只允许外界读取,但是不允许修改的情况,则通常只提供get方法而不提供set方法。
注意2:成员变量名的命名以下划线开头,get方法名不需要带下划线,使用下划线开头有两个好处:(1)与get方法的方法名区分开来;(2)可以跟一些其他的局部变量区分开来,下划线开头的变量,通常都是类的成员变量。
 
 
 
继承
 
  1. 基本概念
继承的好处:
 
     (1)抽取出了重复的代码
     (2)建立了类和类之间的联系
 
继承的缺点:
        耦合性太强
 
          2. 代码:
/**********书************/
@interface Book : NSObject
{
     int _name;
     int _author;  //作者
}
- (void)setName:(int)name;
- (int)name;
@end
 
@implementation Book
- (void)setName:(int)name
{
     _name = name;
}
- (int)name
{
     return _name;
}
@end
 
/**********人************/
@interface Person : NSObject
{
     int _age;
     double _weight;
     Book *book;  //组合关系
}
 
- (void)study;
+ (void)eat;
@end
//方法实现部分省略了
 
/**********学生************/
@interface Student : Person  //Student 继承了Person
- (void)liuDog; //遛狗
@end
 
          3.继承和组合
继承的适用场合:
(1)当两个类拥有相同的属性和方法时,就可以将相同的属性和方法抽取到一个父类中。
(2)当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类(考虑),在这种情况下,也可以考虑使用组合。
继承:###是xxx,如狗是动物,可以让狗继承动物类
组合:###拥有xxx,如学生有书,可以让书这个类作为学生类的属性
 
          4.继承的使用注意
(1)编译器从上往下执行,所以在子类前面至少应该要有父类的声明;
(2)OC中不允许子类和父类拥有相同名称的成员变量名;
(3)OC中的子类可以拥有和父类相同名称的方法,在子类调用时,优先去自己的内部寻找,如果没有则一层一层的往上找;
提示:重写即子类重新实现了父类中的某个方法,覆盖了父类以前的实现。
示意图:一共有三个类,Person类继承了NSObject类,Student类继承了Person类。
 
 
 
 
创建一个Student *s=[[Student alloc] init];
此时会把Student类和这个类的父类加载进内存。
提示:每个类中都有一个super class指针,该指针指向自己的父类。对象中有一个isa指针,该指针指向调用该对象的类。
 
 
多态
 
多态对于面向对象思想来说,个人感觉是真的很重要,他对以后的编写代码的优雅方式也是起到很重要的作用,其实现在很多设计模式中大部分都是用到了多态的特性,其实多态说白了就是:定义类型和实际类型,一般是基于接口的形式实现的,不多说了,直接看例子吧:
打印机的例子
抽象的打印机类Printer
Printer.h

1. //  
2. //  Printer.h  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import <Foundation/Foundation.h>  
10.   
11. @interface Printer : NSObject  
12.   
13. - ( void) print;  
14.   
15. @end  
就是一个简单的方法print
Printer.m

1. //  
2. //  Printer.m  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "Printer.h"  
10.   
11. @implementation Printer  
12.   
13. - ( void)print{  
14.     NSLog(@"打印机打印纸张");  
15. }  
16.   
17. @end  
实现也是很简单的



下面来看一下具体的子类
ColorPrinter.h
 
1. //  
2. //  ColorPrinter.h  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "Printer.h"  
10.   
11. //修改父类的打印行为  
12. @interface ColorPrinter : Printer  
13. - ( void)print;  
14. @end  



ColorPrinter.m

1. //  
2. //  ColorPrinter.m  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "ColorPrinter.h"  
10.   
11. @implementation ColorPrinter  
12.   
13. - ( void)print{  
14.     NSLog(@"彩色打印机");  
15. }  
16.   
17. @end  

在看一下另外一个子类

BlackPrinter.h
 
1. //  
2. //  BlackPrinter.h  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. # import "Printer.h"  
10.   
11. @interface BlackPrinter : Printer  
12.   
13. - ( void)print;  
14.   
15. @end  

BlackPrinter.m

1. //  
2. //  BlackPrinter.m  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "BlackPrinter.h"  
10.   
11. @implementation BlackPrinter  
12.   
13. - ( void)print{  
14.     NSLog(@"黑白打印机");  
15. }  
16.   
17. @end  



这里我们在定义一个Person类,用来操作具体的打印机
Person.h

1. //  
2. //  Person.h  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import <Foundation/Foundation.h>  
10.   
11. #import "ColorPrinter.h"  
12. #import "BlackPrinter.h"  
13.   
14. //扩展性不高,当我们需要添加一个新的打印机的时候还要定义对应的一个方法  
15. //所以这时候就可以使用多态技术了  
16.   
17. @interface Person : NSObject{  
18.      NSString *_name;  
19. }  
20.   
21. //- (void) printWithColor:(ColorPrinter *)colorPrint;  
22.   
23. //- (void) printWithBlack:(BlackPrinter *)blackPrint;  
24.   
25. - ( void) doPrint:( Printer *)printer;  
26.   
27. @end  



Person.m
 
1. //  
2. //  Person.m  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import "Person.h"  
10.   
11. @implementation Person  
12.   
13. /* 
14. - (void) printWithColor:(ColorPrinter *)colorPrint{ 
15.     [colorPrint print]; 
16. } 
17.  
18. - (void) printWithBlack:(BlackPrinter *)blackPrint{ 
19.     [blackPrint print]; 
20. } 
21.  */  
22.   
23. - ( void) doPrint:( Printer *)printer{  
24.     [printer print];  
25. }  
26.   
27. @end  

再来看一下测试代码:
main.m
 
1. //  
2. //  main.m  
3. //  07_DynamicDemo  
4. //  
5. //  Created by jiangwei on 14-10-11.  
6. //  Copyright (c) 2014年 jiangwei. All rights reserved.  
7. //  
8.   
9. #import <Foundation/Foundation.h>  
10.   
11. #import "Person.h"  
12. #import "BlackPrinter.h"  
13. #import "ColorPrinter.h"  
14.   
15. int main( int argc,  const  charchar * argv[]) {  
16.      @autoreleasepool {  
17.           
18.          Person *person =[[Person alloc] init];  
19.           
20.          ColorPrinter *colorPrint = [[ColorPrinter alloc] init];  
21.          BlackPrinter *blackPrint = [[BlackPrinter alloc] init];  
22.           
23.         //多态的定义  
24.         /* 
25.         Printer *p1 = [[ColorPrinter alloc] init]; 
26.         Printer *p2 = [[BlackPrinter alloc] init]; 
27.          
28.         [person doPrint:p1]; 
29.         [person doPrint:p2]; 
30.          */  
31.           
32.         //通过控制台输入的命令来控制使用哪个打印机  
33.          int cmd;  
34.          do{  
35.             scanf("%d",&cmd);  
36.              if(cmd == 1){  
37.                 [person doPrint:colorPrint];  
38.             } else  if(cmd == 2){  
39.                 [person doPrint:blackPrint];  
40.             }  
41.         } while (1);  
42.           
43.     }  
44.      return 0;  
45. }  



下面就来详细讲解一下多态的好处
上面的例子是一个彩色打印机和黑白打印机这两种打印机,然后Person类中有一个操作打印的方法,当然这个方法是需要打印机对象的,如果不用多态机制实现的话(Person.h中注释的代码部分),就是给两种打印机单独定义个操作的方法,然后在Person.m(代码中注释的部分)中用具体的打印机对象进行操作,在main.m文件中,我们看到,当Person需要使用哪个打印机的时候,就去调用指定的方法:
 
1. [person printWithBlack:blackPrint];//调用黑白打印机  
2. [person printWithColor:colorPrint];//调用彩色打印机  
这种设计就不好了,为什么呢?假如现在又有一种打印机,那么我们还需要在Person.h中定义一种操作这种打印机的方法,那么后续如果在添加新的打印机呢?还在添加方法吗?那么Person.h文件就会变得很臃肿。所以这时候多态就体现到好处了,使用父类类型,在Person.h中定义一个方法就可以了:
 
1. - ( void) doPrint:( Printer *)printer;  
这里看到了,这个方法的参数类型就是父类的类型,这就是多态,定义类型为父类类型,实际类型为子类类型

1. - ( void) doPrint:(Printer *)printer{  
2.     [printer print];  
3. }  
这里调用print方法,就是传递进来的实际类型的print方法。

1. Printer *p1 = [[ColorPrinter alloc] init];  
2. Printer *p2 = [[BlackPrinter alloc] init];  
3.           
4. [person doPrint:p1];  
5. [person doPrint:p2];  
这里的p1,p2表面上的类型是Printer,但是实际类型是子类类型,所以会调用他们自己对应的print方法。


 
从上面的例子中我们就可以看到多态的特性很是重要,当然也是三大特性中比较难理解的,但是在coding的过程中,用多了就自然理解了,没必要去刻意的理解。


总结
这篇文章主要介绍了类的三大特性:封装、继承、多态,这三个特性也是后面学习面向对象的重要基础。
 
 
 
 

转载于:https://www.cnblogs.com/Mrzeng-616/p/4233475.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值