Header file
1 @interfacestudent:nsobject{2 intAge ;3 intheight;4}//the declaration interval for the member variable, which must be declared here5 6- (int) Age;//it was getage, but OC's habit was to name the Get method with a variable.7- (void) Setage: (int) NewAge;8 //the function of multi-shape parameter is very special.9- (void) Setage: (int) NewAge Andheight: (int) Newheight;Ten @end//the end tag of the class must be written
the corresponding m files are :
1 #import "Student.h"2 @implementationStudent3 4- (int) age{5 returnAge ;6 }7- (void) Setage: (int) newage{8Age =NewAge;9 }Ten- (void) Setage: (int) NewAge Andheight: (int) newheight{ OneAge =NewAge; AHeight =Newheight; - } - @end
object creation and method invocation :
//OC Create objects in 2 steps, first call the static parameterless function Alloc request memory, call the static parameterless function init initialization//1. Student *stu = [Student alloc];//just to divide the object into memory space//2. Stu = [Stu init];//really create objects//the above 2 steps are generally abbreviated as:Student *stu =[[Student alloc] init];//Set[Stu Setage: -]; [Stu Setage: -Andheight: -];//GetNSLog (@"Age is %i", [Stu Age]); [Stu release];//object is finished using to free memory
How to construct an object
1 @interfacestudent{2 int_age;//Standard notation3 int_no;4 }5- (void) Setage: (int) age;6- (int) age;7- (void) Setno: (int) No;8- (int) No;9 //Construction MethodTen- (ID) Initwithage: (int) Age Andno: (int) No; One @end
The corresponding M file:
1#include"Student.h"2 @implementationStudent3 4- (int) age{5 return_age;6 }7- (void) Setage: (int) age{8_age =Age ;9 }Ten //... One //Implementing a Construction method A- (ID) Initwithage: (int) Age Andno: (int) no{ - //The following wording is not rigorous - //Self = [super init]; the //_age = age; - //_no = no; - if(self=[Super Init]) { -_age =Age ; +_no =No; - } + returnSelf ; A } at @end
OC Basic Syntax