------
Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
匿名类的优点:可以简化代码,方便方法调用。
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int _speed;
}
-(void)start;
-(void)stop;
@end
@implementation Car
-(void)start{
NSLog(@"car->speed:%d",_speed);
}
-(void)stop{
NSLog(@"car->stop");
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//Car *che=[Car new];
// che->_speed=200;
// [che start];
// [che stop];
匿名类访问变量
可以使用匿名类访问实力变量[Car new],
但是再次访问[Car new],会重新申请一个car new存储空间。
[Car new]->_speed = 250;
NSLog(@"_speed = %d",
[Car new]
->_speed);
匿名类可以调用方法
[[Car new] stop];
[car new] 等同于 [ [car alloc] init]
alloc作用 申请空间,init 为初始化。
如:
[ [Car new] stop];
[ [ [car alloc] init] stop];
[pool drain];
return 0;
}
匿名类的优点:可以简化代码,方便方法调用。
缺点:匿名对象实例化以后,只能正确的使用成员变量一次。