//object-c的开发起步
//一、student.h文件
#import
@interface Student : NSobject { int age; }
-(int)getAge; -(void)serAge:(int)newAge; //同时设置两个变量 -(void)setAge:(int)newAge andNo:(int)newNo;
@end
//二、student.m文件 #import "Student.h" @implementation Student
-(int)age { return age; } -(void)serAge:(int)newAge { age = newAge; }
//同时设置两个变量 -(void)setAge:(int)newAge andNo:(int)newNo; { age = newAge; no = newNo; }
@end
//三、main.m文件 int main(int argc, const char * argv[]) { @autoreleasepool { //创建一个Student对象 //1.调用一个静态方法alloc分配内存 //暂时把id当成任何对象,返回的id类型对象 //*stu代表是个stu对象 //Student *stu = [Student alloc]; //2.调用一个动态方法init进行初始化 //stu = [stu init]; //分配内存并初始化 Student *stu = [[Student alloc] init]; //[stu setAge:100]; //int age = [stu age]; //NSLog(@"age is %i", age); //同时设置两个变量 [stu setAge:17 andNo:1]; NSLog(@"age is %i and no is %i", [stu age], [stu no]); //释放对象 [stu release]; } }