您的位置:首页 > 移动开发 > IOS开发

小白学开发(iOS)OC_封装_成员变量(set get方法)(2015-07-24)

2015-07-25 13:25 387 查看
 //

//  main.m

//  封装(成员变量)

//

//  Created by admin on 15/7/25.

//  Copyright (c) 2015年 admin. All rights reserved.

//  这里主要介绍成员变量的封装,进而引出set和get方法

/*

   1. 封装的目的:

     同set方法封装属性,可以过滤掉一些不符合逻辑的数据,保证了我们数据的正确性

     提高了代码的健壮性

     

   2. 封装的好处:

     将变化隔离

     提高代码的重用性

     提高了代码的安全性

 */

/*

   3. 成员变量以下划线开头好处

     > 可以和局部变量区分开来

     > 可以和方法的参数区分开来

     > 只要你打下划线就可以找到成员变量

     > ios程序员都按照这个规则区写,提高了代码的阅读性

 */

#import <Foundation/Foundation.h>

#import "Gun.h"

int main(int argc, const char * argv[]) {

    Gun *g = [Gun new];

/* 

    这里直接给成员变量赋值,而在声明的时候并非@public不能访问,会出现错误

*/

//    g->_bulletCount = -20;    //  而且像子弹数,如果直接赋值那么像-20这样的就是脏数据

//    g->_color = kIcolorBlack;

//    g->_modle = @"P8";

/*

    下面通过set方法给成员变量赋值

 */

    [g setBulletCount:-20];

    [g setColor:kIcolorBlack];

    [g setModle:@"iphone4"];

/*

    下面通过get方法 取出成员变量的值

 */

    IColor resultColor = [g color];

    NSString *result = [g modle];

    int resultCount = [g bulletCount];

    

    NSLog(@"resultColor = %u", resultColor);

    NSLog(@"result = %@", result);

    NSLog(@"resultCount = %d", resultCount);

//  或者

4000
    NSLog(@"resultColor = %u", [g color]);

    NSLog(@"result = %@", [g modle]);

    NSLog(@"resultCount = %d", [g bulletCount]);

/*

    注意: 在开发当中有时候只提供get方法,,该属性称之为只读属性

          有时候只提供set方法,该属性就称之为只写属性

 */

    return 0;

}

//

//  Gun.h

//封装_成员变量 
//

//  Created by admin on 15/7/25.

//  Copyright (c) 2015年 admin. All rights reserved.

//

#import <Foundation/Foundation.h>

typedef enum

{

    kIcolorWhite,

    kIcolorBlack

}IColor;

@interface Gun : NSObject

{

//  @public 这里将成员变量设置成public类型,则表示成员变量对外界是公开的,不安全,所以一般不使用@public

//          为了解决这个问题,OC中对成员变量使用了set方法来给成员变量赋值,使用get方法将成员变量取出

    

    int _bulletCount;   // 子弹数

    NSString *_modle;   // 型号

    IColor _color;      // 颜色

    NSString *_serial;  // 序列号

}

/*

    以下声明成员变量的set方法和get方法

*/

/*

    1. set方法     格式: - (void)setBulletCount:(int)bulletCount;

        > 它一定是对象方法,因为只有对象方法才能访问成员变量,而这里要将成员变量传入进行修改赋值

        > 一定有返回值,且类型为void

        > 一定以set开头,set后面跟上成员变量的名称去掉下划线,并且首字母大写

        > 一定有参数,并且参数类型和成员变量类型一样,参数名称和成员变量名称去掉下划线相同

*/

- (void)setBulletCount:(int)bulletCount;

- (void)setModle:(NSString *)modle;

- (void)setColor:(IColor)color;

- (void)setSerial:(NSString *)serial;

/*

    2. get方法    格式: - (int)bulletCount;

        > 一定是对象方法

        > 一定有返回值,因为这里要将值取出,并且返回值类型和成员变量类型一致

        > 方法名和成员变量名去掉下划线一致

        > 一定没有参数

*/

- (int)bulletCount;

- (NSString *)modle;

- (IColor)color;

- (NSString *)serial;

@end

//

//  Gun.m

//封装_成员变量 

//

//  Created by admin on 15/7/25.

//  Copyright (c) 2015年 admin. All rights reserved.

//

#import "Gun.h"

@implementation Gun

- (void)setBulletCount:(int)bulletCount

{

//  这里可以对成员变量做一些限制,过滤脏数据

    if (bulletCount < 0) {

        bulletCount = 0; //  如果子弹数小于0,那么为了不让子弹数变为负数,这里判断后赋值为0

    }

    _bulletCount = bulletCount;

}

- (int)bulletCount

{

    return _bulletCount;

}

- (void)setModle:(NSString *)modle

{

    _modle = modle;

}

- (NSString *)modle

{

    return _modle;

}

- (void)setColor:(IColor)color

{

    _color = color;

}

- (IColor)color

{

    return _color;

}

- (void)setSerial:(NSString *)serial;

{

    _serial = serial;

}

- (NSString *)serial

{

    return _serial;

}

@end

]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息