//声明
@interface Student : NSObject
{
//成员变量的声明
int age;
}
//方法的声明
//set方法
-(void)setAge:(int)newAge;
//get 方法
-(int)age;
//学习的方法
-(void)study;
@end
//实现
import “Student.h”
//方法的实现
@implementation Student
//set方法的是实现
-(void)setAge:(int)newAge
{
if (newAge<=0) {
newAge=1;
}
age=newAge;
}
//get 方法的实现
-(int)age
{
return age;
}
//学习方法的实现
-(void)study
{
NSLog(@”%d岁的学生在学习”,age);
}
@end
//main函数
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//创建对象
Student *p = [Student new];
//给成员变量赋值
[p setAge:10];
//调用对象的学习的方法
[p study];
//中括号里面调用的是set方法的成员变量
NSLog(@”学生的年龄是%d岁”,[p age]);
}
return 0;
}