OBJECTIVE-C Study Notes (19)--Mutual invocation of object methods and class methods

Source: Internet
Author: User



In fact, the OC object method (minus method) and the class method (plus method) are not independent of each other, they can also be inextricably linked, today we look at their mutual invocation of the problem. This example is based on the People class.



(a) Object method invocation class method:



(1) The first two methods are declared in People.h, respectively:

-(void) show;

+ (void) showw2;



(2) Then implement two methods in People.m respectively, and call the class method in the object method, the calling method is also performed with "class name method name".

-(void) show {

    NSLog (@ "I am an object method, I was called!");
    [People show2]; // Call class method
}

+ (void) show2 {

    NSLog (@ "I am a class method, I was called!");
// [[People alloc] show];
}

(3) Finally, call the object method in main.m:
People * people = [[People alloc] init]; // Note that it is not called directly in main.m
        [people show];
        

(4) The subsequent output results are as follows. The content in the object method is output first, and then the content in the class method is output. The result is as expected. The class method was successfully called in the object method.
.

(Two) class methods call object methods

(1) First declare two methods in People.h:

-(void) show;

+ (void) showw2;

(2) Implement two methods in the People.m file and call the object method in the class method, paying attention to how it is called. [People alloc] is equivalent to instantiating an object, using this object to call the show object method;

-(void) show {

    NSLog (@ "I am an object method, I was called!");
}

+ (void) show2 {

    NSLog (@ "I am a class method, I was called!");
    [[People alloc] show];
}

(3) Finally call the class method show2 in mian.m.
[People show2];

(4) The output is as follows. The content of the class method is output first, and then the content of the object method is output, which is in line with our expectations:
.


         In short, the mutual invocation of the minus method and the plus method makes our code more flexible and easier to program.
Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

Objective-C study notes (nineteen)-object methods and class methods call each other

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.