目录:
- 掌握方法的定义及调用流程
- 掌握继承的用法
- 多态的了解
学方法之前我们可以看看c语言的的方法定义过程
//C语言的函数格式
//返回值 函数名 (参数列表)
void function();
void function(int a, int b);
oc的方法格式
oc的方法分为: + 和 - 方法
- 实例方法
1.方法的调用
[实例化对象 方法名];
2.方法书写
1)、 - (返回值类型)方法名;
2)、 - (返回值类型)方法名:(参数类型)参数名;
3)、 - (返回值类型)方法名参数描述1:(参数类型)参数名1
....;
方法格式视图:
方法的声明
我们先创建一个Human类
Human.h
@interface Human : NSObject{
NSString *_name; //姓名
NSInteger _age; //年龄
double _height;//高度
}
//自定义初始化方式
- (instancetype)initWithName:(NSString *)name
Age:(NSInteger)age
Height:(double)height;
//自定义方法
/*实例方法*/
//无返回值,无参数
- (void)sayHi;
//无返回值,有一个参数
- (void)giveMoney:(NSInteger)money;
//无返回值,有多个参数
- (void)sumWithNumber:(NSInteger)number Another:(NSInteger)another;
//有返回值,无参数
- (NSString *)drinkTea;
//有返回值,有参数
- (NSString *)drinkWithWater:(NSString *)water;
//有返回值,多个参数
- (NSString *)drinkWithWater:(NSString *)water
Tea:(NSString *)tea;
@end
方法的实现
每一个方法的声明必然需要一个方法的实现,一个方法可以不声明直接调用,但是不能不实现直接使用。
实现需要写在@implementation关键字和@end之间
方法就是类或者对象的行为(动作),每一个方法结束,必须产生某个结果,可能是值的传递,可能是动作的执行,也可能是算法。
Human.m
@implementation Human
- (instancetype)init
{
self = [super init];
if (self) {
NSLog(@"重写INIT方法");
}
return self;
}
- (instancetype)initWithName:(NSString *)name
Age:(NSInteger)age
Height:(double)height{
self = [super init];
if (self) {
_name = name;
_age = age;
_height = height;
}
return self;
}
- (void)sayHi{
NSLog(@"Hi!");
}
- (void)giveMoney:(NSInteger)money{
NSLog(@"他给了我%ld",money);
}
- (void)sumWithNumber:(NSInteger)number
Another:(NSInteger)another{
NSLog(@"这两个数的和%ld",number + another);
}
- (NSString *)drinkTea{
return @"红茶";
}
- (NSString *)drinkWithWater:(NSString *)water{
return [NSString stringWithFormat:@"我要喝%@",water];
}
- (NSString *)drinkWithWater:(NSString *)water
Tea:(NSString *)tea{
NSString *string = [NSString stringWithFormat:@"我要喝%@泡的%@",water,tea];
return string;
}
@end
方法调用
main.m
Human *human1 = [[Human alloc] init];
//自定义初始化方法
Human *human2 = [[Human alloc] initWithName:@"kiki" Age:20 Height:1.88];
NSLog(@"%@",human2);//不写description方法,打印的是内存地址
//自定义实例方法
[human1 sayHi];
[human1 giveMoney:100];
[human1 sumWithNumber:10 Another:10];
NSString *teaName = [human1 drinkTea];
NSLog(@"%@",teaName);
NSString *teaName1 = [human1 drinkWithWater:@"龙井"];
NSLog(@"%@",teaName1);
NSString *teaName2 = [human1 drinkWithWater:@"农夫山泉" Tea:@"铁观音"];
NSLog(@"%@",teaName2);
类方法
类型标志为 + 号,表示这个抽象的群体去做某件事情。最常用的类方法是创建对象的便利构造方法。
1.方法调用
[类名 方法名];
2.方法书写(基本和实例方法一样,只是改了符号)
+ (类名)方法名
+ (类名)方法名:(参数类型)参数名
便利构造初始化方法
类方法的声明
Human.h
//类方法
+ (Human *)human;
//便利构造方法
+ (Human *)humanWithName:(NSString *)name
Age:(NSInteger)age
Height:(double)height;
+ (NSString *)postTEA:(NSString *)tea;
+ (void)test;
类方法的实现
Human.m
//类方法
+ (Human *)human{
Human *human = [[Human alloc] init];
return human;
}
+ (Human *)humanWithName:(NSString *)name
Age:(NSInteger)age
Height:(double)height{
Human *human = [[Human alloc] initWithName:name Age:age Height:height];
return human;
}
+ (NSString *)postTEA:(NSString *)tea{
return [NSString stringWithFormat:@"dd%@",tea];
}
+ (void)test{
NSLog(@"test");
}
类方法的调用
Human *human3 = [Human human];
NSLog(@"%@",human3);
Human *human4 = [Human humanWithName:@"jimi" Age:22 Height:1.60];
NSLog(@"human4 = %@",human4);
[Human test];
多参数的方法
- 方法可以有0个或多个参数
如果有参数,每个参数之前需要有一个参数描述,其后添加冒号,再加上参数类型和形参名
如果有多个参数,则需在每个参数之前添加其参数描述
方法调度程序
对象收到消息后,如何来调用这些方法?
实例变量的访问
我们以往的初始化方法:
- (id)initWithName:(NSString *)theName age:(int)theAge;
有没有想过如果我的年龄传入-1,这合法吗?
现实生活中,该值无效,所以我们要对年龄age实例变量做保护措施,添加set()方法。
接下来我们学习setter,getter方法。
setter与getter方法
对于实例变量值的存和取,分别对应setter和getter两个实例方法,增加方法后,可在本类或者其他类的方法中使用这2个方法;
我们现在在Human.h文件中添加成员变量
@interface Human : NSObject{
NSString *_name; //姓名
NSInteger _age; //年龄
double _height;//高度
}
//setter,getter方法对成员变量进行赋值和取值
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSInteger)age;
- (void)setHeight:(double)height;
- (double)height;
-
@end
Human.m
/对于实例变量的存和取
//setter,getter 方法
- (void)setName:(NSString *)name{
_name = name;
}
- (NSString *)name{
return _name;
}
- (void)setAge:(NSInteger)age{
if (age <= 0) {
_age = 18;
NSLog(@"年龄不合法,默认18岁");
}else{
_age = age;
}
}
- (NSInteger)age{
return _age;
}
- (void)setHeight:(double)height{
_height = height;
}
- (double)height{
return _height;
}
接下来我可以在main.m中使用setter,和getter方法
怎么使用呢?
其实setter,getter最大用处就是让我们可以通过点语法访问成员变量并赋值。
main.m
//setter,getter
//set方法是赋值即存值,get方法是取值
human3.name = @"tom";//点语法在等号左边调用的时setter方法,在右边调用的时get方法
NSLog(@"setName = %@",human3.name);
human3.age = -2;
NSLog(@"setAge = %ld",human3.age);
description方法
- 子类可以重写父类方法,NSObject有一个方法为descrption
该方法返回NSString,用于描述该对象- 当使用NSLog(@”%@“,对象)时,会默认调用该对象的description方 - 如果我们不重写此方法会默认调用NSObject的该方法,返回形如:<类名:地址>的
- 子类中重写该方法可以返回自定义对象信息