类方法自定义创建实例

//添加自定义属性年龄

@interface : Person : NSObject

@property(assign,nonamotic)NSUintger age;

+ (instance)initWithAge:(NSUIngter)age;

@end



@implement Person

//可以根据typeof()括号里面的变量,自动识别变量类型并返回该类型。

+ (instance)personWithAge:(NSUIngter)age{

    __weak typeof(self) weakSelf = self;

    return [[self alloc] initWithAge:age];

}

- (instance)initWithAge:(NSUIngter)age{

    if(self = [super init]){

            self.age = age;

    }

    return self;

}

@end

你可能感兴趣的:(类方法自定义创建实例)