[Objective-c 基础 - 2.2] OC弱语法、类方法

时间:2023-03-09 00:35:29
[Objective-c 基础 - 2.2] OC弱语法、类方法
A.OC弱语法
1.在运行的时候才会检查方法的声明和实现
2.没有声明只有实现的方法也能正常运行,只要在调用之前定义即可
3.类的声明必须存在,否则会出现运行时错误
B.类方法
1.是类名调用的方法
2.使用加号修饰的方法
3.类方法和对象方法可以重名
4.对象方法和类方法都允许多次声明,都不允许多次定义
5.类方法不能访问实例变量
 #import <Foundation/Foundation.h>

 @interface Person : NSObject
- (void) test;
- (void) test:(int) ab;
+ (void) test; @end @implementation Person
- (void) test
{
NSLog(@"调用了对象方法test");
} - (void) test:(int) a
{
NSLog(@"%d", a);
} + (void) test
{
NSLog(@"调用了类方法test");
} @end int main()
{
[Person test]; Person *p = [Person new];
[p test];
[p test:];
return ;
}