I. Introduction to Grammar 1. Class
In OC, you typically use two files to describe a class, one is an. h file, and one is a. m file that loves you:
The 1>:.h file is a declaration file for a class that declares variables and methods. The declaration of the class uses the keywords @interface and @end.
The 2>:.m file is an implementation file for the class that implements the method declared in the. h class. The implementation of the class uses the keywords @implementation and @end.
2. Methods
The Declaration and implementation of the 1> method must begin with a + or-
- + denotes class method (static method)
- -Represents an object method (dynamic method)
2> All method scopes declared in. h are of the public type and cannot be changed
3. Member variables
There are 3 common scope of member variables:
1> @public can be accessed globally
2> @protected can only be accessed within the class and in subclasses
3> @private can only be accessed within the class
II. Declaration and implementation of the class
Declaration file for the 1.student.h class
1 #import<Foundation/Foundation.h>2 3 @interfaceStudent:nsobject {4 5 intAge ;6 @public7 intNo;8 intscore;9 Ten @protected One floatheight; A - @private - floatweight; the } -- (int) age; -- (void) Setage: (int) NewAge; -- (void) Setage: (int) NewAge:(float) Newheight; + @end
1> Line 3rd: Declare a class student with @interface, followed by: Representing Inheritance and nsobject
2> Line 5th: Defines a member variable of type int age,age The default scope is @protected, which can be accessed within the student class and in subclasses
3> Line 16th: Declares the Get method of age, the method name is called Age,oc the name of the Get method is the same as the member variable. -Indicates that this is a dynamic method (+ represents a static method), the dynamic method belongs to the object, and the static method belongs to the class.
4> 18 Rows: The method name of the method is setage:andheight:
2.STUDENT.M implementation file
1 #import "Student.h"2 3 @implementationStudent4 5- (int) Age {6NSLog (@"Age ...");7 returnAge ;8 }9 Ten- (void) Setage: (int) NewAge { OneNSLog (@"setage ..."); AAge =NewAge; - } - the- (void) Setage: (int) NewAge:(float) Newheight { -Age =NewAge; -Height =Newheight; - } + - @end
Lines 5th and 10th and Line 15 implement the methods declared in the Student.h
Iii. Creation of objects
1 #import<Foundation/Foundation.h>2 #import "Student.h"3 intMainintargcConst Char*argv[])4 {5 6 @autoreleasepool {7 8 //Insert code here ...9Student *stu =[[Student alloc]init];TenStu.age = the; OneNSLog (@"......"); A - intAge1 =Stu.age; -NSLog (@"......"); the -Stu->no = -; -[Stu Setage: -]; - intAge =[Stu Age]; + -NSLog (@"Student No=%i,age is=%i,age1=%i",stu->no,age,age1); +NSLog (@"Hello, world!."); A [Stu release]; at } - return 0; -}
1> Line 9th: Create a Student class object, allocate space and initialize
2> the 10th row gets and sets the value of age, equivalent to calling [Stu Setage:15] and int age = [Stu Age]. The essence of point syntax is function invocation
2> Line 16th: Public member variable No, if not a public variable, cannot be accessed directly like this. Note Access: Object---member variable
Iv. Summary
1,-Represents an object method (dynamic method), + Represents a class method (static method)
2. The essence of syntax is GET or set function call
Declaration and implementation of the "OC Foundation" 01-Class