Dark Horse Programmer--objective-c class method and object method, difference, and invocation-my opinion

Source: Internet
Author: User



------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------



Class method.



1. Features:



Start with a plus sign (+),



Call Format: [class name Method name]



The benefits of class methods: No need to create an instance object, access directly through the class name, save space and improve efficiency



2. Precautions:



Class methods can have the same name as instance methods and do not affect each other






Class methods can inherit from the parent class, and subclasses can override class methods//, such as the Description class method






Class methods and instance methods are declared in interface and implemented in implementation.






The class method is called by the class name, and the instance method is called by the object.






The self is used in the class method, and the caller class is the self reference.






You cannot use member variables of a class in a class method






You cannot use self to call between a class method and an object method






Summary self: Who calls the current method, self represents who



3. Other object methods can be called in an object method



1) Create an object in the current object method (which can be an instance object of the current class or an instance object of another class), and invoke the method with the newly created object



2) You can use self



3) The object is passed as an argument to the method and can be invoked using the passed object.






4. Methods of other classes can be called in a class method



1) The class method can be called directly using the class name (or other class name)



2) You can use self



5) Object methods can be called in a class method



1) The object is passed as a parameter to the method.



2) You can create an object






6. Comparison of class methods and Object methods



Object methods start with a minus sign, you can use member variables



Class methods start with a plus sign and cannot use member variables






7. Class method Usage Scenarios



If we call a method that does not need to use a member variable, then we can declare the method as a class method



The following code is a validation of some of the above knowledge points


main.m

#import <Foundation / Foundation.h>
#import "Dog.h"

int main (int argc, const char * argv []) {
     @autoreleasepool {

         // 1. Call the eat class method by class name
         [Dog eat];

         // 2. Call the jump method
         Dog * dog = [Dog new];
         [Dog jump: dog];

// [dog bark];

     }
     return 0;
}

Dog.h


#import <Foundation / Foundation.h>

@interface Dog: NSObject
// Class methods are declared in the interface just like instance methods.
+ (void) eat;

+ (void) run;

+ (void) jump: (Dog *) dogDog;

-(void) bark;

-(void) sleep;

@end

Dog.m
#import "Dog.h"

@implementation Dog
+ (void) eat // Class methods and object methods are implemented in @implemention
{
     NSLog (@ "the dog is eating" ";
     // 1. Call a class method in a class method Call by class name
// [Dog run];

     // 2. Call the class method in the class method via self
     [self run];
}

+ (void) run
{
     NSLog (@ "the dog has run");
}

+ (void) jump: (Dog *) dogDog
{
     NSLog (@ "the dog is jumping");
     [dogDog bark]; // Invoke object methods in class methods Objects are passed as method parameters


}

-(void) bark
{
     NSLog (@ "the dog is barking");

     // 1. class method called
// [Dog run];

     // 2. Call the object method
// [self bark];
}

-(void) sleep
{
     NSLog (@ "The dog is tired and is going to sleep");
}
@end














The difference between a function and an object method



-(void) run;



(1) The implementation of the object method in @[email protected]






The declaration of an object method can only be written in the middle of @[email protected]






(2) Object methods begin with a-number






(3) Object methods can only be called by objects






(4) The function belongs to the entire file, can be written anywhere in the file, including @[email protected], but written in @[email protected] will not be recognized, the function of the declaration can be inside the main function can also be outside the main function.






(5) Object method belongs to object all






function: void run () {



}



(1) All functions are parallel.



(2) function has no affiliation



(3) The use of the time can be directly called



(4) You cannot access member variables in an object






Relationship between an object and a method



(1), the object as a parameter of the method


-(void) displayPerson: (Person *) person {

     NSLog ("Name:% @", person-> name);

}











(2), the return value of the object as a method


-(Person *) changePerson: (Person *) person {

     person-> name = @ "tang monk";

     return person;

}





Dark Horse Programmer--objective-c class method and object method, difference, and invocation-my opinion


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.