做⼀个班级信息程序,包含4个⾃定义的类:OurClass、Teacher、 Student、Person,并实现方法.

  1. #import "Teacher.h"  
  2. @interface OurClass : NSObject  
  3. @property (nonatomic, retain) Teacher *tea;  
  4. @property (nonatomic, retain) NSMutableArray *arr;  
  5. @end</span>  

 


  1. #import "OurClass.h"  
  2. @implementation OurClass  
  3. - (NSString *)description  
  4. {  
  5.     return [NSString stringWithFormat:@"%@",_arr];  
  6. }  
  7. - (void)dealloc  
  8. {  
  9.     NSLog(@"班级空间释放");  
  10.     [_tea release];  
  11.     [_arr release];  
  12.     [super dealloc];  
  13. }  
  14. @end  
  15. </span>  

  1. @interface Person : NSObject  
  2. {  
  3.     NSString *_name;  
  4.     NSInteger _age;  
  5. }  
  6. @property (nonatomic, retain) NSString *name;  
  7. @property (nonatomic) NSInteger age;  
  8. @end</span>  

  1. <span style="font-size:18px;">@interface Teacher : Person  
  2. - (NSNumber *)exam:(NSString *)course;  
  3. @end</span>  


  1. <span style="font-size:18px;">@implementation Teacher  
  2. - (NSNumber *)exam:(NSString *)course  
  3. {  
  4.     NSNumber *score = [NSNumber numberWithFloat:arc4random() % 101];  
  5.     return score;  
  6. }  
  7. - (void)dealloc  
  8. {  
  9.     NSLog(@"老师空间释放");  
  10.     [_name release];  
  11.     [super dealloc];  
  12. }  
  13. @end</span>  


  1. <span style="font-size:18px;">@interface Student : Person  
  2. @property (nonatomic, retain) NSMutableDictionary *score;  
  3. //初始化  
  4. - (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSMutableDictionary *)score;  
  5. //比较年龄大小  
  6. - (NSComparisonResult)compareByAge:(Student *)anStudent;  
  7. @end</span>  


  1. @implementation Student  
  2. - (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSMutableDictionary *)score  
  3. {  
  4.     self = [super init];  
  5.     if (self) {  
  6.         self.name = name;  
  7.         self.age = age;  
  8.         self.score = score;  
  9.     }  
  10.     return self;  
  11. }  
  12. - (NSComparisonResult)compareByAge:(Student *)anStudent  
  13. {  
  14.     if ([self age] > [anStudent age]) {  
  15.         return NSOrderedDescending;  
  16.     }else if([self age] == [anStudent age]){  
  17.         return NSOrderedSame;  
  18.     }else {  
  19.         return NSOrderedAscending;  
  20.     }  
  21. }  
  22. - (NSString *)description  
  23. {  
  24.     return [NSString stringWithFormat:@"%@,%ld,%@",_name,_age,_score];  
  25. }  
  26. - (void)dealloc  
  27. {  
  28.     NSLog(@"*******");  
  29.     [_name release];  
  30.     [_score release];  
  31.     [super dealloc];  
  32. }  
  33. @end  
  34. </span>  


  1. #import "Student.h"  
  2. #import "OurClass.h"  
  3. int main(int argc, const char * argv[])  
  4. {  
  5.   
  6.     @autoreleasepool {  
  7.         NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@87,@"yuwen", nil];  
  8.         NSMutableDictionary *dic2 = [NSMutableDictionary   dictionaryWithObjectsAndKeys:@94,@"yuwen", nil];  
  9.         NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@76,@"yuwen", nil];  
  10.         Student *stu1 = [[Student alloc] initWithName:@"xiaoming" age:23 score:dic1];   // 0 -1  
  11.         Student *stu2 = [[Student alloc] initWithName:@"xiaohong" age:35 score:dic2];  // 0 -1  
  12.         Student *stu3 = [[Student alloc] initWithName:@"xiaofang" age:17 score:dic3];   //0 - 1  
  13.         NSMutableArray *stuArr = [[NSMutableArray alloc] initWithObjects:stu1,stu2, nil];    // 0 -1  
  14.         [stu1 release];  
  15.         [stu2 release];  
  16.         OurClass *class = [[OurClass alloc] init];   // 0 - 1  
  17.         class.arr = stuArr;  // 1 - 2  
  18.         [stuArr release];// 2 - 1  
  19.         NSLog(@"%@",class);  
  20.         //更换老师  
  21.         Teacher *teacher = [[Teacher alloc] init];  // 0 - 1  
  22.         class.tea = teacher;   // 1 - 2  
  23.         [teacher release];     // 2 - 1  
  24.         //添加学生  
  25.         [class.arr addObject:stu3];  
  26.         NSLog(@"%@",class);  
  27.         [stu3 release];  
  28.         //移除学生  
  29.         [class.arr removeObject:stu3];  
  30.         NSLog(@"%@",class);  
  31.         //学生年龄比较  
  32.         NSComparisonResult result = [stu1 compareByAge:stu2];  
  33.         NSLog(@"%ld",result);  
  34.         //设置学生成绩  
  35.         NSNumber *num = [teacher exam:@"yuwen"];  
  36.         NSLog(@"分数为%@",num);  
  37.         [class release];  
  38.     }  
  39.     return 0;  
  40. }  
  41. </span>  
posted @ 2015-05-04 18:26  5436  阅读(701)  评论(0编辑  收藏  举报