OBJECTIVE-C Study Notes (18)--declaration, definition and invocation of object methods and class methods

Source: Internet
Author: User

In the program design, the use of functions undoubtedly greatly simplifies the writing of code, improve the efficiency of code operation, reduce duplication of code. So let's take a look at the method in detail. This example also takes the people class as an example.

(a) Code one:

(1) First declare a method in People.h, the method is an object method, that is, the normal method, preceded by a minus sign.

-(void) show;


(2) Implement this method in PEOPLE.M:

-(void) show{

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

}


(3) Call this method in MAIN.M:

People *people = [[People alloc] init];

[People show];


(4) The result of the final output:

(5) Summary: The instantiation of objects in OC and the method call are very different from the C language. In particular, the method is called with brackets [] on both sides. In Object instantiation [[People alloc] init] is also a call to a method.


(b) Code two: class method

(1) First declare a class method in People.h, preceded by a + plus sign.

+ (void) show2;


(2) Implement this method in PEOPLE.M and implement similar object method.

+ (void) show2{

NSLog (@ "I am a class method, I was called up! ");

}


(3) Call in main.m, note that it is called with the class name and does not need to instantiate the object.

[People show2];


(4) Output results together with the object method:


.


(5) Summary, object methods and class methods only in the declaration of the time before the symbol is not the same, at the same time when the call is different, is a certain function of the code block.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OBJECTIVE-C Study Notes (18)--declaration, definition and invocation of object methods and class methods

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.