OC-class definition and oc-Definition
We have already introduced a program HelloWorld: Keep in OC.
The biggest difference between OC and C is that it has the Object-Oriented function. When it comes to object-oriented, we have to talk about the class concept. If we have learned Java, so there is no stranger to the concepts of classes and objects, Because Java is a very pure object-oriented design language. I will not elaborate on the concepts of classes and objects here. There are various explanations on the Internet. Let's take a look at how to define a class in OC and how to create an object by instance.
First, create a New project. The system will automatically create the main. m File. At this time, right-click the project and select New File ..
Select Source in OS X, select Cocoa Class, and click Next.
Click Next
Enter the name of the following class, which is similar to that in Java. Click next:
We can see that a Person class is created, but this is a little different from Java. A class consists of two parts, and a Class header file is Person. h (usually used for definition in this file), and the other is Person. m file. This is generally the content defined in the header file. This is also a continuation of the project structure in C language. Generally, header files are only responsible for defining functions, and. c files are responsible for implementing specific functions. This is similar.
Next let's take a look at the class definition format in the Person. h header file:
// Person. h // 02_ClassDemo /// Created by jiangwei on 14-10-10. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ interface Person: NSObject {// define attributes (global variables/instance variables) in {} // Note: Only definitions cannot be assigned, because the Declaration operation is only performed here, the value cannot be assigned // with an underscore. to distinguish the local variable NSString * _ name; // name, the default value is nil: NULL pointer. You can view the definition: # define nil NULL NSInteger _ age; // age, default value: 0} // definition method //-: instance method, +: Class Method // format: Method description (return type) method name: (parameter type) parameter Name // note that the method name is shopping:, not shopping. Note-(void) shopping :( float) price; // define the method to set the attribute value (note setXXX) -(void) setName :( NSString *) name;-(void) setAge :( NSInteger) age; // get the attribute value of the method (note that it is not getXXX, but a direct attribute name, because getXXX has other functions in OC)-(NSString *) name;-(NSInteger) age; // The method definition of multiple parameters // The method name consists of the following two parts: setName: setAge:-(void) setName :( NSString *) name setAge :( NSInteger) age; // or method name: setName:, which can be omitted later, however, it is not recommended to use //-(void) setName :( NSString *) name :( NSInteger) age in this method; // class method // The attribute cannot be accessed in this method, because the attribute belongs to the object, not the class + (Person *) newPerson; @ end
First, we can see that the keywords defined by the class are:
@ Interface
The general class definition format is:
@ Interface Class Name: NSObject {
Define attributes
}
Definition Method
Here are some notes:
1. Any class in OC inherits NSObject, which is a concept of Object in Java. Therefore, import
# Import <Foundation/Foundation. h>
In fact, the Object in Java is in the java. lang. * package, but Java does not import this package, because the class loader of the Object class decides that it does not need to be imported.
2. The Inheritance symbols here are the same as those in C ++. Use the colon:
3. Define the attributes of the class in the {} class. The attributes mentioned here are not the same as those in Java. In OC, fields defined in {} are the attributes of the class, in Java, the getXXX/setXXX method is called attribute. At the same time, you must note that the attribute name defined in the class in OC starts with an underscore: "_". Why do you need to do this? It is to distinguish between the local variables and their names, of course, we generally start with m in Java or use other methods. Java does not have such strong specifications, but OC strongly recommends this method. Pay attention to this, when defining the setting method, the general style is setXXX, but the method for defining the value is XXX rather than getXXX. This is also different from Java, as for why this is done, the methods starting with "get" are already useful and will be discussed later. Pay attention to it here.
4. the attribute definition is followed by a simple definition method.
The following describes how to define a method:
The definition method in OC is very different from the Java language, so it is hard to remember here. If it is not commonly used, let's take a look at the Definition Format of the method:
Method description (return type) method name: (parameter type) parameter name
1. There are two methods:
One is minus sign: "-", indicating that this method is an object method, which can only be called using an object instance.
One is the plus sign: "+", indicating that this method is a class method, that is, it can be called directly using a class without the need to instantiate the object, similar to the static method in Java
2. Return Type: this is simple and similar to the Java language.
3. Method Name: The method name is different from the method name in Java. Let's look at an example:
// Note that the method name is shopping:, not shopping. Note that-(void) shopping :( float) price;
The method name for this method is
Shopping: contains the colon. Let's look at another example:
// Method definition of multiple parameters // method name consists of setName: setAge:-(void) setName :( NSString *) name setAge :( NSInteger) age;
For multi-parameter method definition, the method name of this method is setName: setAge:
4. (parameter type) parameter name: this is also very different from Java. It feels like it is the opposite of what is defined in Java. He enclosed the type with brackets. The object type in OC is different from that in Java. The object type in OC is equivalent to the pointer definition style (class name *) in C.
Of course, the @ end keyword is used at the end of the class definition.
After reading the definition of the class, the following describes the specific implementation of the class:
Person. m
/// Person. m // 02_ClassDemo /// Created by jiangwei on 14-10-10. // Copyright (c) 2014 jiangwei. all rights reserved. // # import "Person. h "@ implementation Person // implementation method-(void) shopping: (float) price {NSLog (@" % @ shopping % f ", _ name, price );} -(void) setName :( NSString *) name {_ name = name;}-(void) setAge :( NSInteger) age {_ age = age;}-(NSString *) name {return _ name;}-(NSInteger) age {return _ age;}-(void) setName :( NSString *) name setAge :( NSInteger) age {_ name = name; _ age = age;} + (Person *) newPerson {return [[Person alloc] init];} @ end
The implementation of the class uses the @ implementation keyword.
In the implemented source file, the header file of the class definition must be imported.
# Import "Person. h"
This form is very similar to the Engineering Structure in C language.
Then some methods defined in the object are implemented. In the implementation file, we can define the attributes defined in the category class. There is no other difficulty, so I will not elaborate on it. The ending mark of the same class: @ end
The class definition and implementation are all completed. Let's take a look at how to use them:
Main. m
//// Main. m // 02_ClassDemo /// Created by jiangwei on 14-10-10. // Copyright (c) 2014 jiangwei. all rights reserved. //// the class definition is placed in. put the implementation of // class in the H file. in the m file, // define the steps of a class // define the class name, write the class attributes, and write the class behavior # import <Foundation/Foundation. h> # import "Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {// create an object // apply for a memory area in the memory // Person * person = [Person alloc]; // call the initialization method, initialize some default data // the previously applied memory may have been used, and there may be previous data. So we need to clear the data. In this case, the default value will be assigned (equivalent to the clearing operation) // person = [person init]; // merge // two memory blocks are applied here: one is the object pointer variable, one is applied by alloc. // we can see that the alloc/init method is a class method. You can view the definition of the NSObject object. Person * person = [Person alloc] init]; // another method is used to create an object. However, this method is not recommended, this method is equivalent to merging the two above. // sometimes we need to separate the two steps above. // Person * person1 = [Person new]; // set the name [person setName: @ "jaingwei"]; // call the instance method shopping [person shopping: 200]; Person * person1 = [[Person alloc] init]; [person1 setName: @ "huangdaoyang"]; [person1 shopping: 300]; // set the values of two attributes at the same time, but this method is not recommended, we recommend that you use the set method [person1 setName: @ "huangdaoyang" setAge: 22]; // get method to call NSString * person_name = [person name]; NSLog (@ "name is % @", person_name); NSString * person1_name = [person1 name]; NSLog (@ "name is % @", person1_name ); // class method call Person * person2 = [Person newPerson]; [person2 setName: @ "shixin" setAge: 22]; [person2 shopping: 400]; // NULL pointer Person * person3 = nil; // No operation is performed, but no NULL pointer exception is reported. [person3 setName: @ "null object" setAge: 22];} return 0 ;}
The method for instantiating an object in OC is:
Person *person = [[Person alloc] init];
First, define a variable for the pointer type style, then call the alloc method of the class, and then call the init method. This is a bit strange. When we define the Person class, the alloc and init methods are not defined. Why is no error reported here? This is very similar to Java, because any class in OC is integrated with the NSObject class, so naturally the two methods are defined by NSObject. In addition, we can see that the alloc method is a class method and the init method is an object method.
At the same time, the method of calling in OC is as follows:
Object method: [object variable method name parameter]
Class Method: [class name method name parameter]
There is a big difference between instantiating an object and Java. You can simply create a new object in Java. Here we need to call two methods: alloc and init
It can be understood as follows: alloc is equivalent to allocating memory space to this object. init calls the constructor of this class to initialize attributes and fields.
Of course, the above instantiation operation can also be changed to two steps:
Person * person = [Person alloc]; // call the initialization method to initialize some default data. // the previously applied memory may have been used and old data may exist. So we need to clear the data. At this time, the default value will be assigned (equivalent to the clearing operation) person = [person init];
However, the above method is recommended in OC.
Object method call, set the name:
// Set the name [person setName: @ "jaingwei"];
Call object methods with multiple parameters, and set both the name and age
// Set the values of two attributes at the same time, but this method is not recommended. We recommend that you use the set method [person1 setName: @ "huangdaoyang" setAge: 22];
Class method call:
// Class method call Person * person2 = [Person newPerson];
Summary
Now, we will introduce how to use the definition and implementation of classes in OC. In the course of learning, we can compare it with the object language on the facade, so we may learn faster.