Declaration and implementation of a class
Declaration of the class
#import <Foundation/Foundation.h>
NSObject allows the class to have the ability to create objects, also included in the foundation framework
@interface Class Name: NSObject
{//@public allows external pointers to indirectly access object internal member variables
@public
member variables;
member variables;
}
Method (Behavior): Method Name parameter return value (Declaration and implementation)
As long as the method of the OC object must be enclosed in the--start of any data type in the OC method (), and only the data type cannot be scrambled
method declaration;
method declaration;
@end
@implementation class Name
Implementing the methods declared in @interface
@end
int main ()
{
Creating Objects with classes
In OC want to execute behavior, write [] [behavior Performer's behavior name]
Defines a pointer variable that will point to an object of a defined type and can only manipulate objects indirectly using pointer variables in OC
Create a new object with the [class name new] directive and repent of the new object itself (new object address)
Class name *p=[class name new];
P-> member Variable =4;//assign a value to the member variable that the P pointer points to
}
The difference between a method and a function
Method
1. The object method is a minus-
2. The Declaration of the object method must be written between @interface and @end
The implementation of the object method must be written between @implementation and @end
3. Object methods can only be called by objects
4. Object method collation \ Object All
Function
1. function can be written anywhere in the file (except between @interface and @end), function is file-owned
2. Function calls do not depend on the object
3. The member variable of an object cannot be accessed directly from the member variable name within the function
The division of labor between @interface and @implementation
@interface's like exposing the outside clock surface
@implementation is like a structure hidden inside the clock.
Common errors
- cannot be nested with each other
- The declaration must be implemented above
- New class must be unloaded outside
- Member variables are only available for objects and cannot be assigned directly to member variables of undefined variables
- Interface declaration does not allow assignment, member variables cannot be duplicated
The member variable cannot be used as a variable in the C language (cannot be modified with static)
6. The declaration is placed before the main () function and cannot be placed in the back, otherwise an undefined error is reported. Implementations can be placed outside the main function
Syntax details
Member variables cannot be initialized in {} and cannot be accessed directly
Method cannot be called as a function
member variable \ Method cannot be decorated with keywords such as static, do not mix with C language (temporarily ignore)
The implementation of the class can be written behind the main function, mainly in front of the declaration.
/*human Name: Person attribute (member variable \ instance variable): Weight, Age behavior (method): Walking, eating*/#import<Foundation/Foundation.h>/*1. Declaration of class declaration * Member Variable * method*/@interfaceperson:nsobject{@publicintAge ;Doubleweight;} - (void) walk;- (void) eat; @end //2. Implementation of Classes@implementation Person//implementing the methods declared in @interface- (void) Walk{nslog (@"%d-year-old,%f-kilogram man has gone a long way", age, weight);} - (void) Eat{nslog (@"%d-year-old,%f-kilogram people are eating.", age, weight);} @end intMain () {//classes are loaded into memory before they are used to create objects Person*p = [PersonNew];p->age = -;p->weight = +; [P eat]; [P walk]; person*P2 = [PersonNew];p 2->age = -;p 2->weight = -; [P2 eat]; [P2 walk];return0;}
The Declaration and implementation of the Dark Horse programmer--oc Class