开发者

Is "self" the only way to call another method of the same instance from an instance method?

I have been reading Apple's "The Objective-C Programming Language", I noticed that if you have say,

Class A /w methods MA and MB, where MA calls MB with "[self MB]".

If you subclass Class A, lets call it SubClass A, and over-ride MB. Made an instance of SubClass A and call MA, it will use the new MB. Now if you call "[super MA]" instead, it will still use the new MB.

Am I correct?

So I was wondering if "self" is the "right" way to go abou开发者_C百科t this, calling other methods in the same instance, or if it's only used for special situations like covering initializers.


Yes, self is the right way to send a message to the same instance that the method is executing on.

One of the big things to keep in mind about the object model of languages like Objective-C is that, conceptually, you are not "calling methods" — you're sending messages. You're telling the object what to do, not how to do it. You shouldn't normally have to think about what precise method will execute when you send a message — the object is expected to respond appropriately. So if somebody has overridden this "MB" method, presumably the new behavior is how he wants the object to respond when it gets an "MB" message. If somebody has overridden "MB" such that it is no longer usable the way the old method was, then that sounds like a bug.


Yes it is. Using self inside your class is the right way to go if you want to call methods that are in the same class. 'self' represents the current instance of the class where you are using it.


self is the right way to go. Every method in Objective-C can be thought of as "virtual" (in C++ parlance).


In SubClass A, you don't need to make an instance, you can access any function of subClass A by sing self. making new object makes a new instance, so it reintialize all the property for that instance. so you can't do any thing right with this.

self always be right for accessing same class methods and property.

Hope now you can understand why self rather than making other new instance.

And [super MA] must call method of class A's MA method, no case in which MB calls for calling MA.

I have test it, there is no bug all OOPs concept follow in objective c you can call super class method by calling method on super keyword.

So Probably you are doing some thing wrong. just check it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜