self类似java中的this,this表示当前对象,但iOS中self即可表示对象,又可表示类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@implementation Student
//在动态方法中使用self则表示当前方法的调用对象
- (void)test {
}
- (void)test2 {
[Student test3]; //可调用类的静态方法
[self test]; //self表示Student对象,可调用动态方法,不能调用静态方法
}
//在静态方法中使用self则表示当前方法的调用类
+ (void)test3 {
}
+ (void)test4 {
[Student test3];
[self test3]; //self表示Student类,可调用静态方法,不能调用动态方法
}
@end

即self代表着当前方法的调用者

文章目录