akuritsu
akuritsu

Reputation: 440

Superclasses for a CCSprite

After some research, I can't find anything on super classes, so I decided to ask here.

Example

@interface superClass : CCSprite
@end

@interface subClass : superClass
@end

How do the two above examples relate to each other? Also, I was shown that you could add a method the the superClass, and then use it in the subClass (how?)?

Upvotes: 0

Views: 101

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46598

CCSprite is the super class of superClass superClass is the super class of subClass

there is two ways of using method in super class, for example

@interface superClass : CCSprite
- (void)doSomething;
- (id)somethingElse;
@end

@implement superClass
- (void)doSomething {
    NSLog( @"do something in super class" );
}
- (id)somethingElse {
    return @"something else in super class";
}
@end

@interface subClass : superClass
- (void)doSomethingTwice;
@end
@implement subClass
- (void)doSomethingTwice {
    [self doSomething];
    [self doSomething];
}
- (id)somethingElse {
    id fromSuper = [super somethingElse];
    return @"something else in sub class";
}
@end

subClass sub = [[subClass alloc] init];
[sub doSomethingTwice];   // this will call `doSomething` implemented is superClass twice
NSLog([sub somethingElse]); // this will call `somethingElse` in super class but return "something else in sub class" because it override it

basically you can just call the method implemented in super class on instance of sub class

and you can override the method in sub class to do something different and/or using [super methodName] to call the method implementation in super class

Upvotes: 1

Related Questions