using code to explain how to create a class, the implementation of a class declaration/class
I. Declaration of the class
//Define an enumeration valueenumcolor{black, white};typedef enumColor Icolor;//Declaration class with @inteface@interfaceCat:nsobject//nsobject is the root class, and there are some basic methods, such as how to create an object{//define Properties /* Note: 1. Properties are generally preceded by "_" 2. Must be defined in curly braces 3. Note The scope of the variable * / @public int_age; Icolor _color;}//definition behavior must be written between @interface and @end, and cannot be written within curly braces/ * Declare Function steps OC method is divided into class method (+) and object Method (-) 1. Define function Structure 2. Whether there is a parameter 3. Note 1. Declares that the behavior of a class must be between @interface and @end, and that it cannot be written in curly braces for all the numbers in the 2.oc method All types need to be enclosed in parentheses 3. If no formal parameter is used ";" End without parentheses, parentheses should be used for data type 4. If the class method is used with +, the object side (which needs to be invoked with an instance) is used-5. Method name naming rules in the Camel 6.OC method, each parameter corresponds to a colon 7. A colon is also part of the method name 8. Multiple miserable can be labeled with a colon before the function, "description" is also part of the method * ///No parameter no return value-(void) Walk;//No parameter with return value-(int) Getage;//parameter has a return value-(int) Eat: (Char*) food;//Multiple parameter main spaces-(void) Eatfood: (Char*) Food Anddrink: (Char*) water;@end//Declaration ended
Two. Implementation of classes
/ * @implementation means implementing a class @end represents the end of a class implementation @implementation only one action is to implement the method declared in @interface * / @implementation Cat / * Implementation method [email protected] to display the method declared in the @interface, the name is the same, only need to be followed by braces 2. Implement function in parentheses * / //No parameter no return value-(int) Walk {NSLog(@"Cat Walk"); }//No parameter with return value-(int) Getage; {//In OC object, access member variables directly by name return_age; }//Parameter no return value-(Char*) Eat: (Char*) Food {NSLog(@'%s ', food);return "OK"; }//Multiple parameters-(void) Eatfood: (Char*) Food Anddrink: (Char*) Water {NSLog("food:%s water:%s", food, water); }@end
Three access sections
intMainintargcConst Char* argv[]) {//Create Objects /* Description: 1. To create an object, the class inherits the NSObject Class 2. To access the instance variable, you need to declare the variable as a @public type before you create the object, first create the class object (Store object method) in the heap to instantiate the object [OBJ N EW] [Cat new] will do three things: 1. Open new storage space in the heap (different from the space of the class object, the space in the heap, and the new one) 2. Initialize member variables (member variables in the heap), assign default values to member variables, LSA (pointer to object) 3. Returns the pointer to the heap address of the address Cat *cat is used to receive a pointer to the return address, a local variable will open up space in the stack, save the pointer to the heap * /Cat *cat = [cat new];//Access properties are the same as pointer access in CCat->_age=4; cat->_color=white;intAge = cat->_age;NSLog("age=%d", age);//Call method with "[]" /* Call method call method in OC There is called Send Message 1.cat object (stack) in the heap find the Las pointer 2.las pointer to the corresponding class object method 3. If there is a member variable in the method (the member variable is not persisted in the class object), the method locates the The member variable of the object that should be (in the heap) */ //No parameter no return value[Cat walk];//No parameter with return value intAge = [Cat getage];NSLog(@"%d", age);//Parameter no return value[Cat Eat:"Mouse"];//Multiple parameters[Cat Eatfood:"Mouse"Anddrink:"Farmer Spring"];return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Declaration, implementation, access to classes in Objective-c