oc基础之封装(二)

来源:互联网 发布:切割大小头怎样编程 编辑:程序博客网 时间:2024/05/16 16:13

例6、设计一个K线类,要求:最高价格和最低价格(可读(get)写(set)),平均价格(只读)。

源代码实现及模块化知识点拓展

模块一:

1.1、声明文件的源代码

.h文件

#import <Foundation/Foundation.h>

@interface kLine : NSObject

//实例变量

{

    float _minPrice;

    float _maxPrice;

    float _avePrice;

}

//set方法的声明

-(void)setMinPrice:(float)minPrice;

-(void)setMaxPrice:(float)maxPrice;


//get方法的声明

-(float)getMinPrice;

-(float)getMaxPrice;

-(float)getAvePrice;


//输出函数的声明

-(void)print;

@end

1.2、知识点拓展
1.2.1
//set方法的声明-(void)setMinPrice:(float)minPrice;-(void)setMaxPrice:(float)maxPrice;
 set方法命名规范:
 1)方法名必须以set开头
   2)set后面跟上成员变量的名称,首字母大写
   3)返回值一定是void
   4)一定要接受一个参数,而且参数类型需要和成员变量的类型一致
   5)形参名不能喝成员变量名一样
1.2.2
//get方法的声明-(float)getMinPrice;-(float)getMaxPrice;-(float)getAvePrice;
  get方法命名规范:
   1)一定有返回值,返回值类型需要和成员变量的类型一致
   4)不接受任何参数
   5)方法名和成员变量名一样

模块二:
2.1、实现文件的源代码
.m文件

#import "kLine.h"

//kLine类的实现

@implementation kLine


//set方法的实现

-(void)setMinPrice:(float)minPrice

{

    //设置最低价格

    _minPrice = minPrice;

}

-(void)setMaxPrice:(float)maxPrice

{

     //设置最高价格

    _maxPrice = maxPrice;

    //求取平均价格

    _avePrice =(_minPrice+_maxPrice)/2;

}


//get方法的实现

-(float)getMinPrice;

{

    //获取最低价格

    return_minPrice;

}

-(float)getMaxPrice;

{

    //获取最高价格

    return_maxPrice;

}

-(float)getAvePrice;

{

    //获取平均价格

    return_avePrice;

}


//输出函数

-(void)print

{

    NSLog(@"最低价格:%.2f,最高价格:%.2f,平均价格:%.2f",_minPrice,_maxPrice,_avePrice);

}

@end

2.2、知识点拓展
2.2.1
//set方法的实现-(void)setMinPrice:(float)minPrice{    _minPrice = minPrice;//设置最低价格}-(void)setMaxPrice:(float)maxPrice{    _maxPrice = maxPrice;//设置最高价格    //求取平均价格    _avePrice =(_minPrice+_maxPrice)/2;}
   setter方法(设置器)
 set方法为对象提供成员变量的值。在set方法的内部也可以对一些不合理的赋值进行筛选过滤。好处:(1)不让数据暴露在外,保证了数据的安全性(2)对设置数据进行过滤。
2.2.2
//get方法的实现-(float)getMinPrice;{    return_minPrice;//获取最低价格}-(float)getMaxPrice;{    return_maxPrice;//获取最高价格}-(float)getAvePrice;{    return_avePrice;//获取平均价格}
    getter方法(设置器)
 在实际开发中,不一定set和get方法都提供,若内部的成员变量只允许访问,不允许修改,则只有get方法,没有set方法。作用:为调用者返回对象内部的成员值。

模块三:
3.1、主函数的源代码

#import <Foundation/Foundation.h>

#import "kLine.h"

int main(int argc,constchar * argv[]) {

    @autoreleasepool {

        //创建一个实例对象

        kLine *p = [kLinenew];

        

        //使用set方法设置kLine类的成员变量的值

        [p setMinPrice:13.12];

        [p setMaxPrice:109.26];

        [p print];

        

        //使用get方法获取成员变量的值

        NSLog(@"最低价格:%.2f,最高价格:%.2f,平均价格:%.2f",[pgetMinPrice],[pgetMaxPrice],[pgetAvePrice]);

    }

    return 0;

}

3.2、知识点拓展
3.2.1
//使用set方法设置kLine类的成员变量的值        [p setMinPrice:13.12];        [p setMaxPrice:109.26];        [p print];
 对象方法的调用---->这部分知识点的详细内容,请见我的第5篇博文《oc基础之类方法与对象方法的使用比较》   
3.2.2
//使用get方法获取成员变量的值        NSLog(@"最低价格:%.2f,最高价格:%.2f,平均价格:%.2f",[pgetMinPrice],[pgetMaxPrice],[pgetAvePrice]);</span>
   NSLog函数---->这部分知识点的详细内容,请见我的第5篇博文《oc基础之类方法与对象方法的使用比较》  
  
二、学习心得
  (1)在这篇博文里,主要通过一个例子讲述传统的封装方法(set方法和get方法),我将在后面的博文讲述用@property关键字实现,可以将2种方法进行比较学习。
   (2)set方法和get方法归根究底就是一种对象方法,只不过在命名规范上给了一些特殊要求,实现功能上做了些规定而已,所以,我感觉如果对象方法学好了,搞定封装就没问题了。

1 0
原创粉丝点击