首页 > 其他 > 详细

02-封装的细节

时间:2015-04-26 18:11:16      阅读:203      评论:0      收藏:0      [点我收藏+]
#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;//*******************************get方法return 的时候带_
}

- (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;
}

  

02-封装的细节

原文:http://www.cnblogs.com/huimotuo/p/4458024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!