1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <Foundation/Foundation.h>
@interface Student : NSObject { //@public int age; } -(void)study; -(void)setAge:(int)newAge; -(int)age;
@end
@implementation Student
-(void)study { NSLog(@"%d岁的学生在学习",age); } -(void)setAge:(int)newAge { if(newAge<-0) { newAge=1; } age=newAge; } -(int)age { return age; }
@end
int main() { Student *stu=[Student new]; //stu->age=20; [stu setAge:-10]; NSLog(@"学生的年龄时%d岁",[stu age]); //[stu study]; return 0; }
|