类方法也继承了吗?

时间:2011-10-26 02:27:33

标签: objective-c inheritance subclass class-method

当我定义一个继承自NSObject的新类时:

@interface Photo : NSObject
{
    NSString* caption;
    NSString* photographer;
}

@property NSString* caption;
@property NSString* photographer;

@end

是新类alloc继承的NSObject中的所有类方法(如Photo)?

1 个答案:

答案 0 :(得分:5)

是的,Photo可以在您NSObject的子类时使用NSObject的任何方法/属性/ ivar / etc(除了声明为@private的iVars除外):

Photo *myPhoto;
myPhoto = [[Photo alloc] init];
// ... Do some myPhoto stuff ...
NSLog(@"Photo object: %@", myPhoto);
NSLog(@"Photo description: %@", [myPhoto description]);
NSLog(@"Photo caption: %@", [myPhoto caption]);
NSLog(@"Photo photographer: %@", [myPhoto photographer]);

有关@private的更多信息 - > SO Question: what-does-private-mean-in-objective-c

NSObject Class Reference