페이지

2012년 10월 4일 목요일

if we added more codes in setter/getter methods, we must override them by ourself in inherited class.


if we added more codes in setter/getter methods, we must override them by ourself in inherited class.


@interface ParentClass:NSObject
@property (nonatomic) int type;
- (void)updateData;
@end

@implentation ParentClass
@synthesize type = _type;

- (void) setType:(int) pType {
    _type = pType;
    
    [self doSomething];   //added codes
}

- (void) updateData
{
    self.type ++;
    NSLog("%d",self.type);
}
- (void) doSomething
{
    NSLog(@"ParentClass:doSomething called!!");
}
@end





@interface ChildClass: ParentClass
@end

@implementation ChildClass
@synthesize type = _type;

- (void) updateData {
    [super updateData];    // doSomething is not called!!
}
- (void) doSomething
{
   NSLog(@"ChildClass:doSomething called!!");
}
@end




changed this.

@implementation ChildClass
@synthesize type = _type;


- (void) setType:(int) pType {
    _type = pType;
    
    [self doSomething];
}


- (void) updateData {
    [super updateData];    // doSomething is called
}

- (void) doSomething
{
   NSLog(@"ChildClass:doSomething called!!");
}

@end





coz, @synthesize make default setter/getter methods by itself. so these methods are different from them that you implemented in super class.


댓글 없음:

댓글 쓰기