Is there a way to call a class method from another method within the same class?
For example:
+classMethodA{ } +classMethodB{ //I would like to call classMethodA here }
To call a function within class with Python, we call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self. distToPoint . self is variable storing the current Coordinates class instance.
To call a class method, put the class as the first argument. Class methods can be can be called from instances and from the class itself. All of these use the same method. The method can use the classes variables and methods.
In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems.
To call an object's method, you must use the object name and the dot (.) operator followed by the method name, for example object.
In a class method, self
refers to the class being messaged. So from within another class method (say classMethodB), use:
+ (void)classMethodB { // ... [self classMethodA]; // ... }
From within an instance method (say instanceMethodB), use:
- (void)instanceMethodB { // ... [[self class] classMethodA]; // ... }
Note that neither presumes which class you are messaging. The actual class may be a subclass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With