2012-10-14 88 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