First, let's take a look at what class methods and object methods are.
Object method:
The object is preceded by "-". such as:-(void) Runner;
Object methods can only be called by objects, such as:
@interface Person:nsobject
(void) Runner;//object method
@end;
int main ()
{
Person *p=[person New];//person is a class that creates an object p.
[P runner];//p Call Runner this method;
return 0;
{
The member variables of our current object can be accessed directly in the object method such as :
@interface Dog:nsobject
{
int _age;//member Variable
}
-(void) dogage;
@end
@implementation Dog
-(void) dogage{
_age=20;//direct access to member variables;
}
@end
Object methods introduced here ...
Class method:
The class method is preceded by "+" such as: + (void) Runner;
Can only be called by a class: for example:
@interface Person:nsobject
+ (void) Runner;//object method
@end;
int main ()
{
[Person Runner];
return 0;
{
Class methods do not need to use member variables, such as 2-digit and:
+ (int) sumN1: (int) n1 andN2: (int) n2;
The class method has the same name as the object method:
You can see that although the method name is the same, one is a class method that is an object method and can also be run.
Dead Loop:
@interface Person:nsobject
{
int age;
}
+ (void) test;
@end
As long as the call in the Mian method is a dead loop
+ (void) test
{
[Person Test];
}
Class methods and object methods can be called to each other:
Class methods and Object methods for iOS development OC