您的位置:首页 > 其它

OC学习(二)--之实例变量可见度和方法

2015-10-23 00:00 197 查看
摘要: 实例变量可见度
方法
setter和getter

一. 实例变量可见度

实例变量可见度对比

可见度特点
public(公有的)实例变量可以在类的外面和内部操作
protected(保护的)实例变量只能在该类和其子类内操作
private(私有的)实例对象只能在该类内访问,能不子类继承,但无法访问
注意:所谓的内部,是指@implementation和@end之间

示例:

@interface Student : NSObject
{
//默认:受保护的
NSString *_name;
//公有的,对外可以直接访问
@public
NSString *_sex;

//private和protected区别?
//保护的,对外不可以直接访问,子类可以访问
@protected
CGFloat _score;

//私有的,对外不可以访问,子类可以继承但不可以访问
@private
NSInteger _num;
}

二. 方法

方法分类:类方法(+),实例方法(-)

类方法:只能类使用 +(id)alloc

实例方法:只能对象使用 - (void)sayHi

方法声明:

- (void)setName:(NSString *)name;
//多个参数形式!!!
//方法类型标志符 (返回值类型)参数形容词1:(参数类型)参数名1 参数形容词2:(参数类型)参数名2;
//方法的所有形容词共同组成方法的名字
//方法形容词可以省略, ':' 一定要有
- (void)setName:(NSString *)name sex:(NSString *)sex;


取值,赋值方法声明

//setter方法 - 间接访问实例变量(写)
- (void)setName:(NSString *)name;
//getter方法 - 间接访问实例变量(读)
//注意命名时不要写成getName
- (NSString *)name ;


取值,赋值方法实现

- (NSString *) name
{
return _name;
}
- (void) setName:(NSString *)name
{
_name = name;
}

自定义初始化

- (id) initWithName:(NSString *)name sex:(NSString *)sex num:(NSInteger)num score:(CGFloat)score
{
_name = name;
_sex  = sex;
_num = num;
_score = score;
return self;
}

#import

导入头文件,即:导入头文件中的内容到当前类。

#import “”导入自定义类,#import <>导入类库中的头文件。

功能类似C语言中的#include,但是可以避免头文件被重复导入。

容易出现循环导入头文件问题。

@class

告诉编译器@class后的字符串作为类名使用,并未导入类的接口内容。

有效避免嵌套循环导入。

三.总结

实例变量有3种常见的可见度:@public、@protected、@private。

@public违背了封装特性,面向对象开发中很少使用;

@protected默认可见度,自己和子类中能使用—>访问实例变量;

@private自己类中能使用—>访问实例变量。

方法是OC的核心,采用消息机制:[receiver message]。“-”message由对象来调用;“+”message由类来调用。

代码:

Student.h

#import <Foundation/Foundation.h>
//@class告诉编译器,后面字符串作类名使用,并未导入类的接口内容
//有效避免嵌套循环导入.
@class A;
@interface Student : NSObject
{
//默认:受保护的
NSString *_name;
//公有德,对外可以直接访问
@public
NSString *_sex;

//private和protected区别?
//保护的,对外不可以直接访问,子类可以访问
@protected
CGFloat _score;

//私有的,对外不可以访问,子类可以继承但不可以访问
@private
NSInteger _num;
}
//setter方法 - 间接访问实例变量(写) - (void)setName:(NSString *)name; //getter方法 - 间接访问实例变量(读) //注意命名时不要写成getName - (NSString *)name ;

- (void)setSex:(NSString *)sex;
- (NSString *)sex;

- (void)setNum:(NSInteger)num;
- (NSInteger)num;

- (void)setScore:(CGFloat)score;
- (CGFloat)score;

//多个参数形式!!!
//方法类型标志符 (返回值类型)参数形容词1:(参数类型)参数名1 参数形容词2:(参数类型)参数名2;
//方法的所有形容词共同组成方法的名字
//方法形容词可以省略, ':' 一定要有
- (void)setName:(NSString *)name sex:(NSString *)sex;
- (void)sumWithNum1:(NSInteger)num1 num2:(NSInteger)num2 num3:(NSInteger)num3;

//重写init方法
- (id) initWithName:(NSString *)name sex:(NSString *)sex num:(NSInteger)num score:(CGFloat)score;

- (void) info;

@end

Student.m

#import "Student.h"

@implementation Student

- (void)setName:(NSString *)name sex:(NSString *)sex
{
_name = name;
_sex = sex;
}
- (void)sumWithNum1:(NSInteger)num1 num2:(NSInteger)num2 num3:(NSInteger)num3
{
NSLog(@"%ld", num1 + num2 + num3);
}

- (NSString *) name { return _name; } - (void) setName:(NSString *)name { _name = name; }

//Sex
- (NSString *) sex
{
return _sex;
}

- (void) setSex:(NSString *)sex
{
_sex = sex;
}

//Num
- (void) setNum:(NSInteger)num
{
_num = num;
}
- (NSInteger) num
{
return _num;
}

//Score
- (void) setScore:(CGFloat)score
{
_score = score;
}

- (CGFloat) score
{
return _score;
}

- (void) info
{
NSLog(@"%@ %@ %.2f %ld", _name, _sex, _score, _num);
}

//重写系统方法,可以不用声明
- (id) initWithName:(NSString *)name sex:(NSString *)sex num:(NSInteger)num score:(CGFloat)score { _name = name; _sex = sex; _num = num; _score = score; return self; }

@end

main.m

#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*--------------------------------------------------------------*/

Student *stu = [[Student alloc] init];

[stu setName:@"河俊"];
[stu setNum:22];
[stu setSex:@"男"];
[stu setScore:98.00];
[stu info];
//NSLog(@"%@ %@ %.2f %ld" , [stu name], [stu sex], [stu score], [stu num]);

[stu setName:@"庆春" sex:@"男"];
//NSLog(@"%@ %@ %.2f %ld" , [stu name], [stu sex], [stu score], [stu num]);
[stu info];

NSString *ns = [stu name];
NSLog(@"%@", ns);
[stu info];

[stu sumWithNum1:3 num2:9 num3:7];

/*---------------------重写init实现初始化--------------------------------------*/
Student *stu2 = [Student alloc];

//在初始化的时候为stu中实例变量赋值
stu2 = [stu initWithName:@"博阳" sex:@"男" num:13 score:99];
[stu2 info];

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