Today's content is class, method definition and implementation
I. Class Definition
# Import <Cocoa/Cocoa. h> // 1
@ InterfaceStudent: NSObject // 2
{
Int age;
NSString * name; // 3
}
@ Property (nonatomic, retain) NSString * name; // 4
@ Property (nonatomic, retain) int age;
-(Id) initName: (NSString *) aName andAge: (int) aAge; // 5
-(Id) inttName: (NSString *) aName; // 6
-(Void) sayHello;
@ End // 1
Note:
# Import tells the Preprocessor to include the header file content in this file. The import in. OC can ensure that the header file will only be included once.
@ Interface Keyword: declare a student class. @ End statement.
Colon: indicates that the parent class is inherited.
NSObject is the memory management used by most objects. It is equivalent to the initialization framework, reflection, and type operations.
NS is short for NextSTEP, indicating that this function comes from the Cocoa toolkit.
Declare global variables, as in C.
Property keywords: Set the attributes of member variables (including read/write, assign, retain, copy, and nonatomic support for multithreading ).
Declare a method in the format of-(Return Value) method keyword 1: (parameter type) parameter name method keyword 2: (parameter type) parameter name ...... (When reading a method, you can first find the method keyword to determine the parameter ).
-The minus sign is the instance method, and the plus sign is the class method.
II. Implementation of Classes
# Import "Student/Student. m"
# Synthesizename; // 1
# Synthesizeage;
@ ImplementationStudent // 2
-(Id) initName: (NSString *) aName andAge: (int) aAge // Implementation Method
{
If (self = [super init]) // 3
{
Self. name = aName;
Self. age = aAge;
}
Return self;
}
-(Id) initName: (NSString *) aName // 4
{
Self = [self initName: aName andAge: 0];
Return self;
}
-(Void) sayHello
{
NSLog ("% @", Hello World); // 5
}
@ End
Note:
# Synthesize Keyword: automatically generates an access method for the member variable according to the @ property setting, so that the point operator can be used to conveniently access the member variable.
@ Implementation indicates the implementation of the class @ end
Self Keyword: similar to this in java, It is a hidden parameter that points to the class of the currently called method.
Super Keyword: Call the method of the parent class.
Self = [superinit] This is not to judge whether self and [superinit] are equal, but whether the initialization is successful. [Super init]: If the parent class is initialized successfully, pass = to self, so that self becomes a non-empty object, which is not false (non-no ).
3. The concept of calling an existing Initialization Method in another initialization method is called designated initializer.
4. nslog is the standard output in OC. When the date, time, and application name are appended to the output. When nslog () is used to output the value of any object, % @ format is used. When this specifier is used, the object uses a method named description to provide its own nslog () format.
% I bool value.
3. Object generation and method call
# Import <Foundation/Foundation. h>
# Import "student/student. H"
Int main (INT argc, const char * argv []) // 1
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; // 2
Student * S1 = [[STUDENT alloc] initname: @ "zhangsan"
Andage: 15]; // 3
[S1 sayhello]; // 4
[S1 realsources]
[Pool drain] //
Return 0;
}
Note:
Int argc, const char * argv []: argv [0] accepts the program name. Subsequent parameter command lines follow user input parameters, argc records several parameters input by the user in the command line.
Create an automatic release pool
Create an object format class name * object name = [[class alloc] init initialization data];
Alloc is a function that returns a pointer to N consecutive character storage units. The alloc function caller can use this pointer to store character sequences, here, we create a memory space for the object and initialize all the data to 0.
Init is a function that assigns values to member variables in the class. There is no difference with the general method, but init is added at the beginning.
4. The method call format [Object Name method name] is similar to the object name. method name;
PS:
BOOL: boolean type. 1 or YES is true. 0 or NO is false.
@ "Indicates that the string after @ is treated as an NSString object.
Id: This is a newly added data type of ObjC. It is a common object type and can store any type of methods.