- 自定义的类如果不想通过 [[Class alloc] init] 来初始化一个类,可以如下
-(instancetype)init
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Must use initWithName: age: gender:" userInfo:nil];
}
或
-(instancetype)init
{
return [self initWithName:@"Roy" age:18 gender:GenderMan];
}
- 通过实现 description 方法对象打印
-(NSString *)description
{
return [NSString stringWithFormat:@"<%@ : %p, %@, %lu,%lu>",[self class],self,_name,(unsigned long)_age,(unsigned long)_gender];
}
网友评论