[IOS development] Object, ios development object
.
I. Class Definition
Class definition needs to be implemented in two parts:
--Interface Section: Define the member variables and methods of the class. The methods are abstract and defined in the header file;
--Implementation: Introduce the header file of the interface to implement the abstract method;
1. Interface Definition
(1) Interface Definition Format
Interface Definition Format:
@ Interface className:SuperClassName
{
Type _ variableName;
......
}
-(Type) methodName: type parameterName;
@ End
--Interface Definition start ID: @ Interface indicates that the interface is defined;
--End ID of the Interface Definition: @ End indicates that the interface definition has ended;
--Interface Name Definition: The class name followed by @ interface;
--Parent Interface: If there is a parent class, write it after: After the interface name;
--Member variable definition: Define member variables in {} after @ interface;
--Member Method: Define the member variable in {} and then define the member method;
(2) member variable definition
.
Note: The Return Value Type and parameter type in the member method must be included in () to distinguish it from the C-language method;
.
Member variables:
--Definition Format: Type member variable name, such as int _ age;
--Define location: Defined in braces in the next line of @ interface, and the variable to the right is defined in braces;
--Available type: Basic type, construction type, various pointer types;
--Naming rules: Generally, we recommend that you start with "_" for member variables and name them in the hump format;
(3) member Method
Member Methods:
--Member method format:Method type identifier (Return Value Type) method name:(Parameter type) parameter name method signature Keyword: (parameter type) parameter name;
--Method identifier: "+" Indicates the class method, and "-" indicates the instance method;
--Method signature keyword: A method name, a parameter label, and a colon;
--Parameter label: Object-C Convention. Apart from the first parameter, a parameter label is created for each parameter;
Meaning of method Signature:
--"Method": Indicates that the method has no parameters;
--"Method :": Indicates that the method has a form parameter;
--"Method: param :": Indicates that the method has two parameters;
(4) sample code of some interfaces
Example:
/*************************************** * *********************************> File Name: student. h> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/14 19:51:16, 2014 Class interface Example ******************************** **************************************** /# import <Foundation/Foundation. h> @ interface Student: NSObject {/** member variables are defined in brackets of the interface declaration. * Note that the member variables start with "_" */NSString * _ name before they begin; int _ age;}/** defines setData: setAge: Method *-indicates that this method needs to be called through the object * setAge is the form parameter tag */-(void) setData: (NSString *) name setAge :( int) age; // defines the speak: method-(void) speak: (NSString *) content; // defines a method without parameters-(NSString *) toString; + (void) fuck;
2. Partial implementation Definition
(1) implement partial definition formats
Partial implementation format:
@ ImplementationClassName
{
Type _ variable;
}
-(Type) methodName: (type) param
{
// Method body
}
@ End
(2) Class Name and parent class
Implement partial classification name requirements: The class name is the same as that of the class interface;
Parent class inheritance: If there is a parent class, you can use "@ implementation Class Name: parent class name;
(3) implement partial member variables and member Methods
Declare member variables: If you declare a member variable in the implementation part, the member variable is not accessible externally and can only be accessed in the implementation part;
Member Methods:
--Implement abstract member Methods: Implements the abstract method defined in the interface, that is, the method body for writing some methods of the interface;
--Declare member Method: Declare the member method in the implementation part. The external access is not allowed. access can only be performed in the implementation part;
(4) Example of an abstract Method
Sample Code:
/*************************************** * *********************************> File Name: student. m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/14 20:04:28 2014 ************************************ ************************************//** import Student here. the h package is enough, because in Student. the h package has been imported to the Foundation. h header file */# import <Student. h> @ implementation Student {// This member variable defined can only be used in the implementation part. It is a hidden member variable; int _ class;} // defines setData: setAge: method-(void) setData: (NSString *) name setAge :( int) age {_ name = name; _ age = age;}-(void) speak: (NSString) content {NSLog (@ "% @", content) ;}// defines a method with a returned value-(NSString *) toString {return [NSString stringWithFormat: @ "name = % @, age = % d", _ name, _ age] ;}+ (void) fuck {NSLog (@ "fuck") ;}@ end
Ii. Object details
1. object definition, creation, and method call
(1) object definition
Variable definition syntax:Type * Object Name;
--Example: For example, the Student type object defined above,Student * student;
(2) create an object
Create object syntax:[[Class name alloc] initialization method], Example[[Student alloc] init];
--Alloc: Alloc is a keyword, indicating that this is the statement for creating objects. It is equivalent to the new in Java and is responsible for allocating memory space and creating objects;
--Initialization Method: All objects inherit a default initialization method init from NSObject;
Another method for creating objects:[Class Name new], Example[Student new];
--Analysis: This is to take care of the habits of Java programmers and is generally not commonly used;
(3) method call
Method call syntax:[Caller Method Name: parameter 1 parameter label: parameter 2...], Example[Stident fuck];
--Caller: It can be an object or a class, the object calls the instance method, and the class call class method;
(4) Example
Sample Code:
--Code:
/*************************************** * *********************************> File Name: studentTest. m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/28 21:58:41 2014 ************************************ ***********************************/# import <foundation/Foundation. h> # import "Student. h "int main (int argc, char * argv []) {@ autoreleasepool {// define the variable Student * student; // create the variable student = [[Student alloc] init]; // method call: Calls class methods. Note that class methods can only be called by classes, but cannot be called by objects. [Student fuck]; // call the method with parameters [student speak: @ "fuck you! "]; // Assign a value to the member variable [student setData: @" Wukong "setAge: 25]; // print the member variable NSString * stuStr = [student toString]; NSLog (@ "Student is: % @", stuStr );}}
--Execution result:
Octopus-2: oc_object octopus $ clang-fobjc-arc-framework Foundation Student. m StudentTest. moctopus-2: oc_object octopus $. /. out 23:22:37. 702. out [415: 507] fuck2014-12-28 23:22:37. 704. out [415: 507] fuck you! 23:22:37. 705 a. out [415: 507] Student is: name = Wukong, age = 25
2. Objects and variables
(1) concepts of objects and pointer Variables
Concept Introduction: Student * student = [[Student alloc] init], which generates the Student object and student variable;
--Object: Creates a Student object;
--Pointer variable: Person is the pointer variable pointing to the object;
(2) storage of objects and pointer Variables
Storage content of objects and variables:
--Object: Stores the member variables of the created Student object;
--Pointer variable: Stores the first address of the object in the heap memory, that is, the first address of the first member variable;
(3) Location of objects and pointer Variables
Storage address of objects and variables:
--Object: Objects are stored in heap memory;
--Pointer variable: Save it in the dynamic storage area of the main () method;
(4) Correspondence between objects and pointer Variables
Correspondence: A pointer can only point to one object, and an object can have multiple pointers pointing to her;
(5) release an object
Memory leakage: If no pointer points to an object, the object must be released; otherwise, memory leakage may occur;
--Solution: Use automatic reference count to place the program in @ autoreleasepool;
3. self keyword
(1) Call the method or variable of the object itself
Call Method: Calls methods or variables of an object;
--Call Method: [Self Method Name: parameter 1 parameter label: parameter 2...];
--Call the member variable itself: Self-> member variable name;
Note:: Self cannot call class methods or class methods. self uses the same method as an object;
(2) Role
Distinguish between member variables and local variables: If the local variable name is the same as the member variable name, use the self-> member variable name to indicate the member variable. The default name is the local variable;
(3) Sample Code
Sample Code: Modify the above Student. m code and run it after compilation. Note that there is a warning. The member variables in the method are the same as the name of the form parameter;
--Code:
/*************************************** * *********************************> File Name: student. m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/14 20:04:28 2014 ************************************ ************************************//** import Student here. the h package is enough, because in Student. the h package has been imported to the Foundation. h header file */# import "Student. h "@ implementation Student {// This member variable defined can only be used in the implementation part. It is a hidden member variable; int _ class;} // defines setData: setAge: method-(void) setData: (NSString *) _ name setAge :( int) _ age {// The keyword self-> _ name = _ name; self-> _ age = _ age;}-(void) speak: (NSString *) content {NSLog (@ "% @", content) ;}// defines a method with a returned value-(NSString *) toString {// The self keyword is used to call the method of this object [self speak: @ "The toString method is being called:"]; return [NSString stringWithFormat: @ "name = % @, age = % d ", _ name, _ age] ;}+ (void) fuck {NSLog (@" fuck ") ;}@ end
--Execution result:
Octopus-2: oc_object octopus $ clang-fobjc-arc-framework Foundation Student. m StudentTest. mStudent. m: 21: 16: warning: local declaration of '_ name' hides instance variable [-Wshadow-ivar] self-> _ name = _ name; ^ Student. m: 22: 15: warning: local declaration of '_ age' hides instance variable [-Wshadow-ivar] self-> _ age = _ age; ^ 2 warnings generated. octopus-2: oc_object octopus $. /. out 00:49:46. 7 28 a. out [558: 507] fuck2014-12-29 00:49:46. 730 a. out [558: 507] fuck you! 00:49:46. 731 a. out [558: 507] is calling the toString method: 00:49:46. 731 a. out [558: 507] Student is: name = Wukong, age = 25
4. id type
(1) id type
Id type: Indicates all object types;
--Example: Id student = [[Student alloc] init];
(2) dynamic binding
Dynamic binding: Class to which the trail object belongs. the class to which the object belongs is determined at runtime, and the call method is determined at runtime, not at compilation;
--Usage: A variable of the id type performs dynamic binding when calling a method;
(3) Sample Code
Sample Code:
--Code:
/*************************************** * *********************************> File Name: studentTest. m> Author: octopus> Mail: octopus_truth.163.com> Created Time: 12/28 21:58:41 2014 ************************************ ***********************************/# import <foundation/Foundation. h> # import "Student. h "int main (int argc, char * argv []) {@ autoreleasepool {// define the variable Student * student; // create the variable student = [[Student alloc] init]; // method call: Calls class methods. Note that class methods can only be called by classes, but cannot be called by objects. [Student fuck]; // call the method with parameters [student speak: @ "fuck you! "]; // Assign a value to the member variable [student setData: @" Wukong "setAge: 25]; // print the member variable NSString * stuStr = [student toString]; NSLog (@ "Student is: % @", stuStr );}}
--Execution result:
Octopus-2: oc_object octopus $ clang-fobjc-arc-framework Foundation Student. m StudentTest. mStudent. m: 22: 16: warning: local declaration of '_ name' hides instance variable [-Wshadow-ivar] self-> _ name = _ name; ^ Student. m: 23: 15: warning: local declaration of '_ age' hides instance variable [-Wshadow-ivar] self-> _ age = _ age; ^ 2 warnings generated. octopus-2: oc_object octopus $. /. out 01:09:23. 5 48 a. out [605: 507] fuck2014-12-29 01:09:23. 550 a. out [605: 507] fuck you! 01:09:23. 550. out [605: 507] is calling the toString method: 01:09:23. 550. out [605: 507] Student is: name = Wukong, age = 252014-12-29 01:09:23. 551. out [605: 507] is calling the toString method: 01:09:23. 551. out [605: 507] id is: name =, age = 23