Is "I" the only way to call another method from one instance from an instance method?

I read Apple's "Objective-C Programming Language", I noticed that if you say

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

If you are a subclass of class A, let's call it SubClass A and over-ride MB. Once you create an instance of SubClass A and call MA, it will use the new MB. Now, if you call "[super MA]", he will still use the new MB instead.

Am I right?

So, I was wondering if the โ€œIโ€ is the โ€œrightโ€ way to do this by calling other methods in one instance or if it is used only for special situations, such as enabling initializers.

+4
source share
4 answers

Yes, self is the right way to send a message to the same instance in which this method is executed.

One of the important things to keep in mind with regard to the object model of languages โ€‹โ€‹such as Objective-C is that you donโ€™t conceptually call the calling methods โ€” you send messages. You tell the object what to do, not how to do it. Usually you should not think about which method will be executed when sending the message - it is expected that the object will respond accordingly. Therefore, if someone overrides this MB method, perhaps the new behavior is how he wants the object to react when he receives the MB message. If someone redefined "MB" so that it is no longer used as the old method, then this sounds like an error.

+3
source

Yes it is. Using self inside your class is the right way if you want to call methods that are in the same class. "self" represents the current instance of the class in which you use it.

+1
source

self is the right way. Each method in Objective-C can be considered as "virtual" (in C ++).

0
source

In SubClass A you do not need to instantiate, you can access any function of subclass A using singing. creating a new object creates a new instance, so it reinitializes the entire property for that instance. therefore, you cannot do anything about it.

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

Hopefully now you can understand why yourself, and not make another new instance.

And [super MA] must call the method of the class A MA method, there is no case when MB calls the MA call.

I have a test, there is no error, all OOPs concepts follow in the c lens, you can call the superclass method by calling the method with the super keyword.

So, maybe you are something wrong. just check it out.

0
source

All Articles