2012-10-14 91 views

回答

1

內部類的方法,self是指表達相對應的Class對象:

+ (NSArray *)allAccounts { 
    NSLog("%@", [self description]); // Will print the name of the class 
    return [self accountsForService:nil error:nil]; 
} 

這是你在實例方法得到,當你調用[self class]同一個對象。

如果您想以多態方式調用class上的方法,這很有用。例如,您可以撥打[[self alloc] init]來創建執行調用的類的新實例。

+0

我會避免在第一句中使用「實例」一詞。這有點混亂和誤導。 – rmaddy

+0

@rmaddy我想你是對的,在這種情況下可能會誤解「實例」一詞。 – dasblinkenlight