封装

 

#import <Foundation/Foundation.h>

 

@interface Student : NSObject

{

    // 成员变量尽量不要用@public

    // @public

    int age;

    

    //@public

    // 只读(readonly):只允许外界访问我的no,不允许外界修改我的no

    int no; // 只需要提供get方法

}

 

//

/*

 set方法

 1.作用: 提供一个方法给外界设置成员变量值,可以在方法里面对参数进行相应过滤

 2.命名规范

 1> 方法名必须以set开头

 2> set后面跟上成员变量的名称,成员变量的首字母必须大写

 3> 返回值一定是void

 4> 一定要接收一个参数,而且参数类型跟成员变量类型一致

 5> 形参的名称不能跟成员变量名一样

 */

- (void)setAge:(int)newAge;

 

/*

 get方法

 1.作用:返回对象内部的成员变量

 2.命名规范:

 1> 肯定有返回值,返回值类型肯定与成员变量类型一致

 2> 方法名跟成员变量名一样

 3> 不需要接收任何参数

 */

- (int)age;

 

- (void)study;

 

@end

 

@implementation Student

 

// set方法的实现

- (void)setAge:(int)newAge

{

    // 对传进来的参数进行过滤

    if (newAge <= 0)

    {

        newAge = 1;

    }

    

    age = newAge;

}

 

- (int)age

{

    return age;

}

 

- (void)study

{

    NSLog(@"%d岁的学生在学习", age);

}

 

@end

 

int main()

{

    Student *stu = [Student new];

    //stu->age = -10;

    //stu->age = 10;

    [stu setAge:10];

     NSLog(@"学生的年龄是%d岁", [stu age]);

   //[stu study];

    return 0;

}

封装的例1 

 

 

 

#import <Foundation/Foundation.h>

 

 

 

typedef enum {

 

    SexMan,

 

    SexWoman

 

} Sex;

 

 

 

 

 

@interface Student : NSObject

 

{/*成员变量的命名规范:一定要以下划线 _ 开头

 

  作用:

 

  1.让成员变量和get方法的名称区分开

 

  2.可以跟局部变量区分开,一看到下划线开头的变量,一般都是成员变量

 

  */

 

    int _no;

 

    Sex _sex;

 

}

 

 

 

// sex的set和get方法

 

- (void)setSex:(Sex)sex;

 

- (Sex)sex;

 

 

 

// no的set和get方法

 

- (void)setNo:(int)no;

 

- (int)no;

 

 

 

@end

 

 

 

@implementation Student

 

 

 

- (void)setSex:(Sex)sex

 

{

 

    _sex = sex;

 

}

 

 

 

- (Sex)sex

 

{

 

    return _sex;

 

}

 

 

 

- (void)setNo:(int)no

 

{

 

    _no = no;

 

}

 

- (int)no

 

{

 

    return _no;

 

}

 

 

 

@end

 

 

 

 

 

int main()

 

{

 

    Student *stu = [Student new];

 

    

 

    [stu setSex:SexMan];

 

    [stu setNo:10];

 

    

 

    [stu sex];

 

    

 

    [stu no];

 

    

 

    return 0;

 

}

 

//封装例2

 

/*

 4.设计一个成绩类

 * C语言成绩(可读可写)

 * OC成绩(可读可写)

 * 总分(只读)

 * 平均分(只读)

*/

#import <Foundation/Foundation.h>

 

@interface Score : NSObject

{

    int _cScore; // C语言成绩

    int _ocScore; // OC成绩

    

    int _totalScore;// 总分

    int _averageScoe; // 平均分

}

 

- (void)setCScore:(int)cScore;

- (int)cScore;

 

- (void)setOcScore:(int)ocScore;

- (int)ocScore;

 

- (int)totalScore;

- (int)averageScore;

 

@end

 

@implementation Score

- (void)setCScore:(int)cScore

{

    _cScore = cScore;

    

    // 计算总分

    _totalScore = _cScore + _ocScore;

    _averageScoe = _totalScore/2;

}

- (int)cScore

{

    return _cScore;

}

 

- (void)setOcScore:(int)ocScore

{

    _ocScore = ocScore;

    

    // 计算总分

    _totalScore = _cScore + _ocScore;

    _averageScoe = _totalScore/2;

}

// 监听成员变量的改变

 

- (int)ocScore

{

    return _ocScore;

}

 

- (int)totalScore

{

    return _totalScore;

}

- (int)averageScore

{

    return _averageScoe;

}

@end

 

 

int main()

{

    Score *s = [Score new];

    

    [s setCScore:90];

    [s setOcScore:100];

    

    [s setCScore:80];

    

    

    int a = [s totalScore];

    

    NSLog(@"总分:%d", a);

    

    return 0;

}

 

posted @ 2016-03-28 10:34  lance.xiang  阅读(119)  评论(0)    收藏  举报