可见度 | 特点 |
---|---|
public(公有的) | 在类的内外部都可以操作 |
protected(系统默认,受保护的) | 只能在类和子类中操作 |
private(私有的) | 只能在该类内访问 |
{
@public
NSString *_perName;
NSString *_perSex;
@protected
NSInteger _perAge;
@private
NSString *_perHobby;
}
OC中的方法可以分为两种
注意:类方法中不能使用实例变量,且只能类使用. 实例方法只能对象使用
- (void) setStuName:(NSString *) stuName
其中
方法名就是方法语句去掉类型表示,返回类型,参数类型,参数名以及空格所剩的部分
即为setStuName:
注意:类中不能出现同名方法.”:”为标示参数,不可以省略,有冒号必须有参数
对实例变量赋值或者取值的时候使用
赋值方法:setter,为设置器
读取实例变量值的方法:getter,为访问器
赋值语句:当对多个语句进行操作的时候,称为赋值语句
Person.h
// 设置器
- (void) setPerName:(NSString *)perName
{
_perName = perName;
}
// 访问器
- (NSString *)perName
{
return _perName;
}
// 赋值语句
- (void) setPerName:(NSString *)perName
setPerSex:(NSString *)perHobby;
Person.m
// 设置器
- (void) setPerName:(NSString *)perName
{
_perName = perName;
}
// 访问器
- (NSString *)perName
{
return _perName;
}
// 赋值语句
- (void) setPerName:(NSString *)perName
setPerSex:(NSString *)perHobby;
{
_perName = perName;
_perHobby= perHobby;
}
注意
如果写声明但是没实现,会在.m文件中出现警告,告诉我们应该把方法补全
无论setter 还是 getter 的内部操作的是实例变量
每一个实例变量都需要一对的setter和getter方法
当我们想调用方法时,使用
[person sayHi]
即为消息机制,对应的就是
[消息的接收者 具体要做的事]
.h
- (id) initWithPerName:(NSString *)perName
perSex:(NSString *)perSex;
.m
- (id) initWithPerName:(NSString *)perName
perSex:(NSString *)perSex;
{
_perName = perName;
_perSex = perSex;
return self;
}
作用:导入头文件的内容到当前的类
其中
注意:用import引入头文件容易引起交叉导入,导致错误
所以这时候应该使用@class ,用@class可以防止交叉引入头文件的问题,系统会把A当做是一个已经存在的类来使用,不会把A.h的内容进行拷贝,就是用了一下A的名,其他不做任何的检测
main.m
// 创建一个对象
// Student *stu = [[Student alloc] init];
// [stu sayHi];
// Student *stu = [[Student alloc] init];
// [stu setStuName:@"商帅" setStuAge:24 setStuHobby:@"UNIQLO" setStuScore:80 setStuSex:@"女" ];
// [stu sayHi];
//
// [stu setStuName:@"abc"];
// NSLog(@"%@",[stu stuName]);
// // 车
// AudiCar *car = [[AudiCar alloc] init];
// // 赋值语句
// [car setCarBrand:@"A8" setCarPrice:@"100w" setCarSpeed:300];
// [car sayHi];
// // setter getter
// [car setCarBrand:@"A6"];
// [car setCarPrice:@"60w"];
// [car setCarSpeed:200];
// NSLog(@"型号%@",[car carBrand]);
// [car sayHi];
// Student *stu = [[Student alloc] initWithStuName:@"商帅" stuAge:24 stuScore:80 stuSex:@"男" stuHobby:@"比美"];
// [stu sayHi];
//
// AudiCar *car = [[AudiCar alloc] initWithCarBrand:@"A4" carPrice:@"30w" carSpeed:200];
// [car sayHi];
AudiCar.h
@interface AudiCar : NSObject
// 三个成员变量,默认可见度
{
NSString *_carBrand;
NSString *_carPrice;
CGFloat _carSpeed;
}
- (void) sayHi;
// 对三个成员变量进行统一赋值的赋值语句
- (void) setCarBrand:(NSString *)carBrand
setCarPrice:(NSString *)carPrice
setCarSpeed:(CGFloat)carSpeed;
// 三个成员变量的设置器
- (void) setCarBrand:(NSString *)carBrand;
- (void) setCarPrice:(NSString *)carPrice;
- (void) setCarSpeed:(NSInteger)carSpeed;
// 访问器
- (NSString *) carBrand;
- (NSString *) carPrice;
- (NSInteger) carSpeed;
// 自定义初始化方法
- (id) initWithCarBrand:(NSString *)carBrand
carPrice:(NSString *)carPrice
carSpeed:(CGFloat)carSpeed;
@end
AudiCar.m
@implementation AudiCar
- (void) sayHi
{
NSLog(@"型号%@,价格%@,速度%g",_carBrand,_carPrice,_carSpeed);
}
// 对三个成员变量进行统一赋值的赋值语句
- (void) setCarBrand:(NSString *)carBrand
setCarPrice:(NSString *)carPrice
setCarSpeed:(CGFloat)carSpeed;
{
_carBrand = carBrand;
_carPrice = carPrice;
_carSpeed = carSpeed;
}
// 三个成员变量的设置器
- (void) setCarBrand:(NSString *)carBrand
{
_carBrand = carBrand;
}
- (void) setCarPrice:(NSString *)carPrice
{
_carPrice = carPrice;
}
- (void) setCarSpeed:(NSInteger)carSpeed
{
_carSpeed = carSpeed;
}
// 访问器
- (NSString *) carBrand
{
return _carBrand;
}
- (NSString *) carPrice
{
return _carPrice;
}
- (NSInteger) carSpeed
{
return _carSpeed;
}
- (id) init
{
_carSpeed = 200;
_carPrice = @"100w";
_carBrand = @"R8";
return self;
}
- (id) initWithCarBrand:(NSString *)carBrand
carPrice:(NSString *)carPrice
carSpeed:(CGFloat)carSpeed;
{
_carBrand = carBrand;
_carSpeed = carSpeed;
_carPrice = carPrice;
return self;
}
@end
Student.h
@interface Student : NSObject
{
@public
NSString *_stuName;
NSString *_stuSex;
@private
NSString *_stuHobby;
NSInteger _stuAge;
@protected
CGFloat _stuScore;
}
- (void) sayHi;
- (void) eat;
- (void) play;
// 对所有成员变量的赋值语句
- (void) setStuName:(NSString *) stuname
setStuAge:(NSInteger)stuAge
setStuHobby:(NSString *) stuHobby
setStuScore:(CGFloat) stuScore
setStuSex:(NSString *) stuSex;
// 设置器
- (void) setStuName:(NSString *)stuName;
- (void) setStuAge:(NSInteger)stuAge;
- (void) setStuSex:(NSString *)stuSex;
- (void) setStuScore:(CGFloat)stuScore;
- (void) setStuHobby:(NSString *)stuHobby;
// 访问器
-(NSString *)stuName;
-(NSInteger)stuAge;
-(NSString *)stuSex;
-(CGFloat)stuScore;
-(NSString *)stuHobby;
// 自定义的初始化方法
- (id) initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuSex:(NSString *)stuSex
stuHobby:(NSString *)stuHobby;
@end
Student.m
@implementation Student
- (void) sayHi
{
NSLog(@"姓名:%@,爱好:%@,性别:%@,岁数:%ld,成绩:%g",_stuName,_stuHobby,_stuSex,_stuAge,_stuScore);
}
- (void) eat
{
NSLog(@"吃饭");
}
- (void) play
{
NSLog(@"要玩");
}
- (void) setStuName:(NSString *) stuname
setStuAge:(NSInteger)stuAge
setStuHobby:(NSString *) stuHobby
setStuScore:(CGFloat) stuScore
setStuSex:(NSString *) stuSex;
{
_stuName = stuname;
_stuAge = stuAge;
_stuHobby = stuHobby;
_stuScore = stuScore;
_stuSex = stuSex;
}
// 设置器
- (void) setStuName:(NSString *)stuName
{
_stuName = stuName;
}
- (void) setStuAge:(NSInteger)stuAge
{
_stuAge = stuAge;
}
- (void) setStuSex:(NSString *)stuSex
{
_stuSex = stuSex;
}
- (void) setStuScore:(CGFloat)stuScore
{
_stuScore = stuScore;
}
- (void) setStuHobby:(NSString *)stuHobby
{
_stuHobby = stuHobby;
}
// 访问器
-(NSString *)stuName
{
return _stuName;
}
-(NSInteger)stuAge
{
return _stuAge;
}
-(NSString *)stuSex
{
return _stuSex;
}
-(CGFloat)stuScore
{
return _stuScore;
}
-(NSString *)stuHobby
{
return _stuHobby;
}
// 初始化
- (id) init
{
_stuName = @"张三";
_stuSex = @"男";
_stuHobby = @"女";
_stuScore = 100;
_stuAge = 20;
return self;
}
- (id) initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuSex:(NSString *)stuSex
stuHobby:(NSString *)stuHobby;
{
_stuName = stuName;
_stuHobby = stuHobby;
_stuAge = stuAge;
_stuScore = stuScore;
_stuSex = stuSex;
return self;
}
@end
于7月16学习,总结于7月18
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011752406/article/details/46941447