实例变量可见度、方法、setter、getter

oc中的消息机制:如果为类方法,消息接收者为类名

                       如果为实例方法,消息接收者为对象名

                        消息带参数给参数,不带就不给

                         消息有返回值,定义相同类型的变量接收,没有就不需要定义变量

 //各实例变量的课件度

    NSInteger _height;

    @public

    NSString *_name;//为什么不默认用@public 修饰实例变量,破坏了封装性,不安全;

    @protected

    NSString *_gender;

    @private

    NSInteger _age; //其所占字节数根据操作系统位数变化而变化;

    

   1. public 是公有的,谁都能访问

   2.  protected 只能在本类以及其子类中访问

   3. private 修饰实例变量只能在本类中被访问 

   4.  package 与框架有关

   5 . 如果不用可见度修饰词修饰实例变量,默认为protecyed

 

//方法里面不需要出现下划线

//为实例变量gender赋值

- (instancetype)init;

 

 

//OC里面不用getGender 做方法名,get由其他作用,但在c++java里,用get做方法名;

//get方法(取值方法);

- (NSString *)getder;

 

 set方法(赋值方法)

-(void) setAge:(NSInteger )age;

- (NSInteger)age;

 

 

自定义初始化方法(带有多参)

- (instancetype)initWithName:(NSString *)name  gender:(NSString *)gender age:(NSInteger )age ;

 

 

 分析方法中各个参数的用途

//- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;

 //- +为方法类型标志符

// replaceObjectAtIndex whitObject,为参数形容词

// NSUInteger)和(id)为参数类型

//indexanObject为参数;

//void)为返回值类型

//方法名是什么:去掉方法类型标志符,去掉参数类型和参数,去掉返回值

 

 

 

 

 

 

 

 

posted on 2015-11-26 20:51  LJ李杰  阅读(190)  评论(0编辑  收藏  举报

导航