一、方法
方法:方法声明(.h),方法实现(.m),方法调用(导入对应的头文件)。
如果方法只声明,未实现。调用方法时,会造成程序崩溃。
在OC中不存在“调用方法”的说法,在OC中是“消息发送”机制,即将方法发送给对象。
1、OC方法类型:
OC中只有两种方法:-实例方法 +类方法
类方法只能类调用: + [类 类方法],在类方法中,不能使用实例变量。
实例方法只能对象调用 - [对象 实例方法]
2、方法语法
方法包含:方法类型、返回值类型、方法名、参数列表(参数类型、参数名)
3、OC中的方法可以分为4类(同C种的函数类型一样)
使用Person类进行说明:
声明一个Person类
@interface Person : NSObject
{
NSString * _gender;
NSInteger _age;
}
a、无返回值,无参数
方法的返回值类型,写在()内
- (void)sleeping;
- (void)sleeping
{
NSLog(@"sleeping");
}
b、无返回值有参数(1个参数)
方法中声明参数的语法 : (参数类型)参数名
如果参数是对象类型,声明参数类型时,必须加*,因为,整个编程过程中是使用指针在操作对象。
如:
- (void)eat:(NSString *)food;
- (void)eat:(NSString *)food
{
// food是方法的参数,类似于函数中的形参,作用是接收传入的数据
NSLog(@"Eating%@", food);
}
c、无返回值有参数(两个参数)
方法名:首字母小写,驼峰命名法,参数修饰符首字母小写,参数名首字母小写
方法名组成:功能描述,参数修饰符(必须要有)
如:
- (void)setName:(NSString *)name andGender:(NSString *)gender;
此方法的方法名为:setName:andGender:
- (void)setName:(NSString *)name andGender:(NSString *)gender
{
// 将参数接收的传入的数据,赋值给实例变量
_name = name;
_gender = gender;
}
d、无返回值有参数(3个参数)
如:
- (void)setName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age;
- (void) setName:(NSString *)name gender:(NSString *)genderage:(NSInteger)age
{
_name = name;
_gender = gender;
_age = age;
}
e、有返回值无参数
方法只能返回一个数据
如:
- (NSString *)getName;
- (NSString *)getName
{
return _name;
}
注:以上声明和实现的方法都称为OC中的一般方法。同一个类中不能有同名的方法。
二、OC中的setter和getter方法
例:声明Phone类
@interface Phone : NSObject
{
NSString * _brand;
NSString * _system;
float _price;
}
1、声明setter方法
给实例变量声明对应的setter方法:目的是为实例变量赋值
/*
1、setter方法是实例方法
2、没有返回值
3、只能有一个参数,跟操作的实例变量有关,只能为一个实例变量赋值。
4、方法声明: -(void)set + 实例变量名(忽略下划线,驼峰命名法)
5、参数类型跟操作的实例变量类型一致
6、参数名:实例变量名(忽略下划线)
*/
- (void)setBrand:(NSString *)brand;
- (void)setPrice:(float)price;
- (void)setSystem:(NSString *)system;
2、声明getter方法:获取实例变量的值
/*
1、getter是实例方法
2、没有参数
3、有返回值,返回值类型与操作的实例变量类型一致
4、方法名:实例变量名(忽略下划线),不要以get开头。
*/
- (NSString *)brand;
- (NSString *)system;
- (float)price;
3、实现声明的setter和getter方法
- (void)setBrand:(NSString *)brand
{
_brand = brand;
}
- (void)setPrice:(float)price
{
_price = price;
}
- (void)setSystem:(NSString *)system
{
_system = system;
}
- (NSString *)brand
{
return _brand;
}
- (NSString *)system
{
return _system;
}
- (float)price
{
return _price;
}
4、在main.m中使用setter和getter方法
int main(int argc, const char * argv[ ]) {
@autoreleasepool {
Phone * p = [ [Phonealloc] init];
[p setBrand:@"iPhone"];
[psetSystem:@"iOS"];
[p setPrice:5888.50];
NSLog(@"Brand is%@, Syatem is %@, Price is %.2f", [p brand], [p system], [p price]);
}
return 0;
}
三、自定义初始化方法init
在init初始化的基础上,继续初始化对象的实例变量。
声明和实现自定义初始化方法:
- (instancetype)initWithBrand:(NSString *)brandandPrice:(float)price;
- (instancetype)initWithBrand:(NSString *)brandandPrice:(float)price
{
self = [super init];
if (self) {
_brand = brand;
_price = price;
}
return self;
}
- (instancetype)initWithBrand:(NSString *)brand andSystem:(NSString*)system;
- (instancetype)initWithBrand:(NSString *)brand andSystem:(NSString*)system
{
_brand = brand;
_system = system;
return self;
}
在main.m中使用自定义的方法:
Phone * p2 = [[Phone alloc] initWithBrand:@"iPhone"andPrice:5888.88];
Phone * p3 = [[Phone alloc] initWithBrand:@"iPhone"andSystem:@"iOS"];
注意:
对象创建和初始化完成后,如果想给实例变量赋值,只能使用setter方法给对象发送消息。禁止再次使用初始化方法。
调用setter和getter为对象赋值:
[p2setBrand:@"samung"];
[p3 setSystem:@"andorid"];
使用NSLog打印初始化的结果:
NSLog(@"Brand is %@", [p getBrand]);
NSLog(@"System is%@", [p getSystem]);
NSLog(@"Price is%.2f", [p getPrice]);
调用初始化方法时,如果不需要给某个实例变量赋值时:
传入数值规则,对象类型:nil,基本数据类型:0。
如:
Phone * p2 = [[Phone alloc] initWithBrand:nil andPrice:5888.88];
Phone * p2 = [[Phone alloc] initWithBrand:nil andPrice:0];