// 对象方法中可以调用其他的对象方法
// 1)当前对象方法中创建对象,使用新创建的对象调用对象方法
// 2)可以使用self
// 3)对象作为方法的参数传递过来,可以使用传递过来的对象调用方法
// 类方法的优点:
// 1)节省内存空间
// 2)提高了效率
// 3)作为工具方法
// 匿名类(匿名对象)
// 1. 使用匿名类访问实例变量(能,只能访问一次)
// 以后再次访问,实际上访问新的空间了
// [Car new]->_speed = 250;
// NSLog(@"_speed = %d", [Car new]->_speed);
// 2. 使用匿名类,可以调用方法
// [[Car new] stop]; // 可以
// [[Car alloc] init] // 等价于 [Car new],alloc是申请空间,init是初始化
// 使用alloc init 的方式调用stop方法
// [[[Car alloc] init] stop];
#import "Car.h"
@implementation Car
- (void)run
{
NSLog(@"chezou %d ", _speed);
}
+ (void)run
{
// 在类方法中不允许访问实例变量
NSLog(@"chezou");
// 在类方法中可以调用其他类方法
// 1)可以直接使用本类类名(或者其他类名)调用方法
[Car run:250];
// 2)可以使用self
[self run:100];
// 在类方法中可以调用对象方法
// 1)对象作为方法的参数传递过来
// 2)可以创建一个对象
}
+ (void)run:(int)speed
{
// 在类方法中不允许访问实例变量
NSLog(@"chezou %d", speed);
}
+ (void)t1
{
// 类方法可以调用其他类方法
// 类方法不能调用自身
}
@end
类方法
最新推荐文章于 2022-05-05 22:57:05 发布