Categories
objective-c

NSObject 类编程

Saler.h //头文件声明声明对外公开的方法 声明公共属性和私有属性

@interface Saler : NSObject {
    @private NSThread *thread;
}
@property NSNumber *ticket;
-(void) startSale;
@end

Saler.m //声明具体方法实现 init为默认的构造函数

#import "Saler.h"

@implementation Saler

-(Saler*) init{
    self = [super init];
    NSLog(@"init saler");
    self.ticket = [NSNumber numberWithInt:20];
    //公共属性用.访问
    return self;
}

-(void) startSale{
    NSThread *thread = [NSThread new];
    self->thread = thread;
    //私有属性用->访问
    [thread initWithTarget:self selector:@selector(run) object:nil];
    [thread start];
}

-(void) run{
    NSLog(@"running");
}

@end

Leave a Reply