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