• 封装


    一.封装

    1.封装的概念:

          封装,顾名思义,就是把什么封起来,装起来.在程序开发中,封装是优化代码,提高代码安全性的一种做法,即是将不必要暴露在外界的代码,封装在一个类中,在外界只调用他.如果看到这里还没有明白的话,那么请往下看:

      比如:你买了一个手机,你可以用手机打电话,发信息,上网等等各种操作,但是你需要知道手机怎么来实现这个功能的吗?当然不用,你只需要知道怎么使用它.再例如在开发中可能会用到第三方库,比如你去Github上下了一个MJRefresh的库,你需要知道这个库是怎么实现的么?不用,你只需要知道你该怎么使用它,这就是封装.

    2.代码:

      这里举例用的是字典转模型数据

    2.1 KXZCar.h

     1 //
     2 //  KXZCar.h
     3 //  封装
     4 //
     5 //  Created by admin on 16/4/28.
     6 //  Copyright © 2016年 admin. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface KXZCar : NSObject
    12 /**
    13  *  汽车名称
    14  */
    15 @property (nonatomic,copy) NSString *name;
    16 
    17 /**
    18  *  汽车的图标
    19  */
    20 @property (nonatomic,copy) NSString *icon;
    21 /**
    22  *  汽车信息
    23  */
    24 @property (nonatomic, copy) NSString *info;
    25 /**
    26  *  产地
    27  */
    28 @property (nonatomic, copy) NSString *country;
    29 //实现字典转模型的方法
    30 // 对象方法
    31 - (instancetype) initWithDict:(NSDictionary *) dict;
    32 
    33 //类方法
    34 + (instancetype) carWithDict:(NSDictionary *) dict;
    35 @end

    2.2 KXZCar.m

     1 //
     2 //  KXZCar.m
     3 //  封装
     4 //
     5 //  Created by admin on 16/4/28.
     6 //  Copyright © 2016年 admin. All rights reserved.
     7 //
     8 
     9 #import "KXZCar.h"
    10 
    11 @implementation KXZCar
    12 - (instancetype)initWithDict:(NSDictionary *)dict
    13 {
    14     if (self = [super init]) {
    15         
    16         self.icon = dict[@"icon"];
    17         self.name = dict[@"name"];
    18         self.country = dict[@"country"];
    19         self.info = dict[@"info"];
    20 //        KVC赋值
    21 //        [self setValuesForKeysWithDictionary:dict];
    22         
    23     }
    24     return self;
    25 }
    26 
    27 + (instancetype)carWithDict:(NSDictionary *)dict
    28 {
    29     return [[self alloc] initWithDict:dict];
    30 }
    31 @end

    2.3 KXZCarGroup.h

     1 //
     2 //  KXZCarGroup.h
     3 //  封装
     4 //
     5 //  Created by admin on 16/4/28.
     6 //  Copyright © 2016年 admin. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "KXZCar.h"
    11 
    12 @interface KXZCarGroup : NSObject
    13 //标题
    14 @property (nonatomic,copy) NSString *title;
    15 //汽车数组
    16 @property (nonatomic,strong) NSArray *cars;
    17 
    18 //提供两个字典转模型的方法
    19 //对象方法
    20 - (instancetype) initWithDict:(NSDictionary *) dict;
    21 //类方法
    22 + (instancetype) carGroupWithDict:(NSDictionary *) dict;
    23 
    24 //加载plist文件,把字典数组转换为模型数组
    25 + (NSArray *) carGroups;
    26 @end

    2.4 KXZCarGroup.m

     1 //
     2 //  KXZCarGroup.m
     3 //  封装
     4 //
     5 //  Created by admin on 16/4/28.
     6 //  Copyright © 2016年 admin. All rights reserved.
     7 //
     8 
     9 #import "KXZCarGroup.h"
    10 
    11 @implementation KXZCarGroup
    12 - (instancetype)initWithDict:(NSDictionary *)dict
    13 {
    14     if (self = [super init]) {
    15         self.title = dict[@"title"];
    16         self.cars = dict[@"cars"];
    17     }
    18     return self;
    19 }
    20 
    21 
    22 + (instancetype)carGroupWithDict:(NSDictionary *)dict
    23 {
    24     return [[self alloc] initWithDict:dict];
    25 }
    26 
    27 
    28 + (NSArray *)carGroups
    29 {
    30     //   1.加载plist
    31     //   获取绝对路径
    32     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
    33     //   读取数组(分组的字典)
    34     NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    35     
    36     //  2.把array中分组的字典转为模型
    37     //  2.1 定义一个可变数组
    38     NSMutableArray *arrayM = [NSMutableArray array];
    39     //  遍历array,把它里面存放的组字典转换为组模型,放到arrayM中
    40     for (NSDictionary *dict in array) {
    41         KXZCarGroup *carGroup  = [self carGroupWithDict:dict];
    42         //      这个字典数组
    43         NSArray *dictArray  = carGroup.cars;
    44         //      把dictArray转换为一个CZCar的模型数组
    45         //      定义一个可变数组用来存放转换后的CZCar模型
    46         NSMutableArray *carsArray = [NSMutableArray array];
    47         //      遍历字典数组dictArray,把字典转换为CZCar的模型
    48         for (NSDictionary *dict in dictArray) {
    49             //        把字典转换为CZCar的模型
    50             KXZCar *car  = [KXZCar carWithDict:dict];
    51             //         把转换后的car添加到carsArray数组中
    52             [carsArray addObject:car];
    53         }
    54         //     把转换后的CZCar的模型数组赋值给carGroup的cars属性
    55         carGroup.cars = carsArray;
    56         
    57         [arrayM addObject:carGroup];
    58     }
    59     // 3.返回组模型数组
    60     return arrayM;
    61 }
    62 @end

    3.plist文件如下:

    4.BCViewController(就是一个普通的ViewController)

    4.1代码如下:

     1 //
     2 //  BCViewController.m
     3 //  封装
     4 //
     5 //  Created by admin on 16/4/28.
     6 //  Copyright © 2016年 admin. All rights reserved.
     7 //
     8 
     9 #import "BCViewController.h"
    10 #import "KXZTableViewCell.h"
    11 #import "KXZCar.h"
    12 #import "KXZCarGroup.h"
    13 
    14 @interface BCViewController ()<UITableViewDelegate,UITableViewDataSource>
    15 @property (nonatomic, strong) UITableView *tableView;
    16 @property (nonatomic, strong) UILabel *headLabel;
    17 @property (nonatomic, strong) UIButton *clickButton;
    18 @property (nonatomic, strong) NSArray *carGroups;
    19 @end
    20 
    21 @implementation BCViewController
    22 
    23 -(NSArray *)carGroups{
    24     if (_carGroups == nil) {
    25         _carGroups = [KXZCarGroup carGroups];
    26     }
    27     return _carGroups;
    28 }
    29 @end

    注:以为只介绍封装,故不发tableView的实现了.

    4.2说明:

    在以上的代码2.4中的38行开始,我定义了一个类方法,在类方法中实现了字典转模型,然后在代码4.1中的23行到28行的懒加载(延时加载)中,判断数组是否为空,如果为空,就调用KXZCarGroup的类方法.在类方法中字典转模型,外界(BCViewController中)我只需要调用一下这个类的方法去达到给数组carGroups赋值的目的,而不用知道里面是怎么去把字典转成模型数据.

    5.总结

    封装就是对一些不想要外界访问到的成员变量,属性和方法进行保护,不让外界知道你内部是怎么实现的.一般在封装的过程中,还可以使用几个关键字:protected(受保护的)-private(私有的)-public(公共的),如有不足,请兄弟们补充.

  • 相关阅读:
    关于document.body.scrollTop用法
    set回顾
    用户登录与注册
    编写通讯录2
    利用字典的特性编写一个通讯录
    shelve模块
    shutil模块
    列表的拓展
    随机生成验证码2
    递归与欧几里得算法结合求最大公约数
  • 原文地址:https://www.cnblogs.com/Xebdison/p/5443000.html
Copyright © 2020-2023  润新知