这儿为了验证Xcode上类簇NSNumber实现过程设计了一个简化版类簇LONumber
#import
<Foundation/Foundation.h>
@interface LONumber : NSObject
@property ( readonly ) int intValue; // 获取对象对应的 int 数据
@property ( readonly ) BOOL boolValue; // 获取对象对应的 bool 数据
// 初始化
- ( instancetype )initWithInt:( int )value;
- ( instancetype )initWithBool:( BOOL )value;
// 便利构造器
+ ( instancetype )numberWithInt:( int )value;
+ ( instancetype )numberWithBool:( BOOL )value;
@interface LONumber : NSObject
@property ( readonly ) int intValue; // 获取对象对应的 int 数据
@property ( readonly ) BOOL boolValue; // 获取对象对应的 bool 数据
// 初始化
- ( instancetype )initWithInt:( int )value;
- ( instancetype )initWithBool:( BOOL )value;
// 便利构造器
+ ( instancetype )numberWithInt:( int )value;
+ ( instancetype )numberWithBool:( BOOL )value;
@end
#import
"LONumber.h"
// 私有子类 __LOCFNumber
@interface __LOCFNumber : LONumber
{
int _intValue; // 保存对象的基本数据类型的数据
}
@end
@implementation __LOCFNumber
// 初始化
- ( instancetype )initWithInt:( int )value {
self = [ super init ];
if ( self ) {
_intValue = value;
}
return self ;
}
//intValue 方法
- ( int )intValue {
return _intValue ;
}
- ( NSString *)description {
return [ NSString stringWithFormat : @"%d" , _intValue ];
}
@end
// 私有子类 __LOCfBoolean
@interface __LOCfBoolean : LONumber
{
BOOL _boolValue; // 保存布尔值
}
@end
@implementation __LOCfBoolean
- ( instancetype )initWithBool:( BOOL )value {
self = [ super init ];
if ( self ) {
_boolValue = value;
}
return self ;
}
- ( BOOL )boolValue {
return _boolValue ;
}
-( NSString *)description {
return _boolValue ? @"YES" : @"NO" ;
}
@end
// 私有子类 LOPlaceholderNumber
@interface LOPlaceholderNumber : LONumber
@end
@implementation LOPlaceholderNumber
- ( instancetype )init {
return nil ;
}
- ( instancetype )initWithInt:( int )value {
return ( id )[[ __LOCFNumber alloc ] initWithInt :value];
}
- (
instancetype
)initWithBool:(
BOOL
)value {
return ( id )[[ __LOCfBoolean alloc ] initWithBool :value];
return ( id )[[ __LOCfBoolean alloc ] initWithBool :value];
}
@end
@implementation
LONumber
//
抽象类
:
不能创建的对象
// 通过重写 alloc 方法 , 在 alloc 开辟空间时指向其私有子类 LOPlaceholderNumber 类型 , 通过此子类开辟空间
+ ( instancetype )alloc {
// 如果调用 alloc 方法的类型不是 LONuber, 则表示不执行类簇创建对象的流程 , 使用根类的 alloc 方法直接为调用 alloc 方法的类型开辟空间
if ( self != LONumber . class ) {
return super . alloc ;
}
// 声明静态的 LOPlaceholderNumber 类型的指针 , 保证只做一次初始化
static LOPlaceholderNumber *number = nil ;
if (!number) {
number = LOPlaceholderNumber . alloc ;
}
return number;
}
// 初始化
//- (instancetype)init {
// return nil;
//}
- ( instancetype )initWithInt:( int )value {
return nil ;
}
- ( instancetype )initWithBool:( BOOL )value {
return nil ;
}
// 便利构造器
+ ( instancetype )numberWithInt:( int )value {
return [[ self alloc ] initWithInt :value];
}
+ ( instancetype )numberWithBool:( BOOL )value {
return [[ self alloc ] initWithBool :value];
}
// 通过重写 alloc 方法 , 在 alloc 开辟空间时指向其私有子类 LOPlaceholderNumber 类型 , 通过此子类开辟空间
+ ( instancetype )alloc {
// 如果调用 alloc 方法的类型不是 LONuber, 则表示不执行类簇创建对象的流程 , 使用根类的 alloc 方法直接为调用 alloc 方法的类型开辟空间
if ( self != LONumber . class ) {
return super . alloc ;
}
// 声明静态的 LOPlaceholderNumber 类型的指针 , 保证只做一次初始化
static LOPlaceholderNumber *number = nil ;
if (!number) {
number = LOPlaceholderNumber . alloc ;
}
return number;
}
// 初始化
//- (instancetype)init {
// return nil;
//}
- ( instancetype )initWithInt:( int )value {
return nil ;
}
- ( instancetype )initWithBool:( BOOL )value {
return nil ;
}
// 便利构造器
+ ( instancetype )numberWithInt:( int )value {
return [[ self alloc ] initWithInt :value];
}
+ ( instancetype )numberWithBool:( BOOL )value {
return [[ self alloc ] initWithBool :value];
}
@end
#import
<Foundation/Foundation.h>
#import "LONumber.h"
int main( int argc, const char * argv[]) {
LONumber *p = [[ LONumber alloc ] initWithInt : 2 ];
NSLog ( @"%@" ,p);
NSLog ( @"%@" , NSStringFromClass (p. class ));
LONumber *q = [[ LONumber alloc ] initWithBool : YES ];
NSLog ( @"%@" ,q);
NSLog ( @"%@" , NSStringFromClass (q. class ));
return 0 ;
#import "LONumber.h"
int main( int argc, const char * argv[]) {
LONumber *p = [[ LONumber alloc ] initWithInt : 2 ];
NSLog ( @"%@" ,p);
NSLog ( @"%@" , NSStringFromClass (p. class ));
LONumber *q = [[ LONumber alloc ] initWithBool : YES ];
NSLog ( @"%@" ,q);
NSLog ( @"%@" , NSStringFromClass (q. class ));
return 0 ;
}