iphone - Objective-C: How to use calling child's version of a method? -
i've run need before. right now, have function called every 0.1 seconds check on how time has elapsed. if exceeds "total time" (which retrieves gettotaltime
function) stops. gettotaltime
method overridden in children. code function called every 0.1 seconds overridden, original method in parent class uses gettotaltime
called using super
, needs call child's method of gettotaltime
instead of own. is, of course, issue. rewrite code parent in each of children, seems stupid. google searching has show solutions in other languages, not in objective-c. there way this? if not, alternatives?
the code function called every 0.1 seconds overridden, original method in parent class uses gettotaltime called using super, needs call child's method of gettotaltime instead of own.
your question difficult parse, sounds you've got this:
@interface : nsobject // "parent" class - (void) timermethod; - (nsdate*) gettotaltime; // other methods necessary @end @interface b : // "child" class // has overrides of a's methods @end @implementation - (void) timermethod { nslog(@"[a timermethod]: time is: %@", [self gettotaltime]); } - (nsdate *) gettotaltime { return [nsdate date]; } @end @implementation b - (void) timermethod { nslog(@"[b timermethod]: time is: %@", [self gettotaltime]); [super timermethod]; } - (nsdate *) gettotaltime { return [nsdate datewithtimeintervalsincenow:3600]; // 3600 == hour }
i think you're concerned b's -timermethod
calls a's -timermethod
, , in turn calls [self gettotaltime]
, think a's -gettotaltime
1 called. rest easy, that's not how inheritance works. if have instance of b , call 1 of b's methods, self
represents pointer instance of b, even in context of inherited method. is, self
points same object in 1 of a's methods. so, if have timer sends -timermethod
instance of b, here's happens:
- b's
-timermethod
gets called. - that method calls [super timermethod], results in call a's
-timermethod
- a's
-timermethod
calls[self gettotaltime]
, , sinceself
pointer instance of b, b's-gettotaltime
called
so, if timer sends message instance of b, should 2 log statements, both of have time 1 hour now:
[b timermethod]: time is: current time + 1 hour
[a timermethod]: time is: current time + 1 hour
if timer sends message instance of a, however, you'll 1 log statement, , it'll have current time:
[a timermethod]: time is: current time
the ability "override" methods in subclasses 1 of key features makes inheritance useful; subclasses can modify or entirely replace behaviors superclass, , code in superclass automatically call overridden method instead of own method in instances of subclass.
so, answer title question...
how use calling child's version of method?
is use self
whenever want method provided current object.
Comments
Post a Comment