0
我对编程知道的一切都表示,实例方法可以调用类方法,但类方法不能调用实例方法。成功称为实例方法的类方法
这个帖子同意... Call instance method from class method
然而,奇迹般地类方法sharedInstance
设法调用实例方法init
。我在想什么?
static iRpDatabase *sharedDatabase;
@implementation iRpDatabase
{
}
+(iRpDatabase*)sharedInstance
{
if(sharedDatabase == nil)
{
sharedDatabase = [[self alloc] init];
}
return sharedDatabase;
}
// this is an instance method, called from class method above.
-(id)init
{
if (self = [super init]) {
someInstanceVariable = XYZ;
[self someInstanceMethod];
}
return self;
}
如果你看看你的例子(我们可以在NSObject的任何子类上看到),你会发现你并没有在self上调用init,而是根据[self alloc]的结果 - 这是一个实例。最佳 – Jef
@Jef你应该发表你的评论问题,而不是这个答案。 – rmaddy