OC 练习题 学生类设置及NSArray自定义排序

这是一道类设计题 主要考没有ARC的情况下 手动写getter setter 手动进行内存管理。


 定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C

 1)    不使用@property,手动编写他们的访问器方法(gettersetter),注意内存管理(手动管理内存)

 2)    增加一个便利构造器(快速构造器)

 3)    使用NSLog输出学生对象时,输出信息格式为:My Name Is XXX  Age Is XXX Score Is XXX

 4)    5个学生对象按照成绩》年龄》姓名优先级排序(成绩相同按照年龄排序,成绩年龄相同按照姓名排序(5个学生的属性值自己随便设定,姓名不考虑中文,按26个大小字母排序))

student.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Student : NSObject{  
  4.     NSString *_name;  
  5.     int _age;  
  6.     int _score;  
  7. }  
  8.   
  9. -(void) setName:(NSString*) name;  
  10. -(NSString*) name;  
  11.   
  12. -(void) setage:(int) age;  
  13. -(int)age;  
  14.   
  15. -(void) setScore:(int) score;  
  16. -(int)score;  
  17.   
  18. -(id) initwithname:(NSString*)name andage:(int)age andscore:(int)score;  
  19. -(void) printinfo;  
  20. +(id) stuwithname:(NSString*)name andage:(int)age andscore:(int)score;  
  21.   
  22. @end  
student.m

  1. #import "Student.h"  
  2. @implementation Student  
  3. -(void)setName:(NSString *)name  
  4. {  
  5.     if (_name!=name) {  
  6.         [_name release];  
  7.         _name=[name retain];  
  8.     }  
  9.     _name=name;  
  10. }  
  11. -(NSString *)name{  
  12.     return _name;  
  13. }  
  14.   
  15. -(void)setage:(int)age{  
  16.     _age=age;  
  17. }  
  18. -(int)age{  
  19.     return _age;  
  20. }  
  21.   
  22. -(void)setScore:(int)score{  
  23.     _score=score;  
  24. }  
  25. -(int)score{  
  26.     return _score;  
  27. }  
  28.   
  29. -(void)dealloc{  
  30.       
  31.     [_name release];  
  32.     [super dealloc];  
  33. }  
  34.   
  35. -(id)initwithname:(NSString *)name andage:(int)age andscore:(int)score{  
  36.       
  37.       
  38.     self=[super init];  
  39.     if (self!=nil) {  
  40.         _name=name;  
  41.         _age=age;  
  42.         _score=score;  
  43.     }  
  44.     return self;  
  45. }  
  46.   
  47. +(id)stuwithname:(NSString *)name andage:(int)age andscore:(int)score{  
  48.     self=[[self alloc]initwithname:name andage:age andscore:score];  
  49.     return self;  
  50. }  
  51.   
  52. -(void)printinfo{  
  53.     NSLog(@"My Name Is %@  Age Is %d Score Is %d",self.name,self.age,self.score);  
  54. }  
  55.   
  56. - (NSComparisonResult)comparestu:(Student *)stu  
  57. {  
  58.     //先按分数  
  59.       
  60.     NSComparisonResult result = [[NSNumber numberWithInt:stu.score]compare:[NSNumber numberWithInt:self.score]];  
  61.     //如果分数相同 按年龄  
  62.     if (result == NSOrderedSame) {  
  63.         result = [[NSNumber numberWithInt:stu.age]compare:[NSNumber numberWithInt:self.age]];  
  64.         if(result == NSOrderedSame) // 如果年龄一样,就按姓名排序  
  65.         {  
  66.             //如果年龄相同按姓名  
  67.             result = [self.name compare:stu.name];  
  68.         }  
  69.     }  
  70.     return result;  
  71. }  
  72. @end  
main.m

  1. #import <Foundation/Foundation.h>  
  2. #import "Student.h"  
  3. int main(int argc, const charchar * argv[]) {  
  4.     @autoreleasepool {  
  5.         Student *S1=[Student stuwithname:@"s1" andage:11 andscore:11];  
  6.         Student *S2=[Student stuwithname:@"s2" andage:22 andscore:22];  
  7.         Student *S3=[Student stuwithname:@"s3" andage:33 andscore:33];  
  8.         Student *S4=[Student stuwithname:@"s4" andage:44 andscore:44];  
  9.         Student *S5=[Student stuwithname:@"s5" andage:55 andscore:55];  
  10.   
  11.         [S1 printinfo];  
  12.         [S2 printinfo];  
  13.         [S3 printinfo];  
  14.         [S4 printinfo];  
  15.         [S5 printinfo];  
  16.   
  17.         NSArray *array = [NSArray arrayWithObjects:S1,S2,S3,S4,S5, nil nil];  
  18.           
  19.   
  20.         NSLog(@"开始排序");  
  21.         NSArray *sortarray=[array sortedArrayUsingSelector:@selector(comparestu:)];  
  22.           
  23.         //NSLog(@"%@",sortarray);  
  24.           
  25.         //遍历数组  
  26.         [sortarray enumerateObjectsUsingBlock:^(Student *stu, NSUInteger idx, BOOLBOOL *stop) {  
  27.             NSLog(@"位置%i的 Name Is %@ Age Is %d Score Is %d",idx+1,stu.name,stu.age,stu.score);  
  28.            }];  
  29.           
  30.         /* 
  31.          输出结果 
  32.         2015-01-29 21:19:48.120 测试题10[2752:378621] 位置1的 Name Is s5 Age Is 55 Score Is 55 
  33.         2015-01-29 21:19:48.120 测试题10[2752:378621] 位置2的 Name Is s4 Age Is 44 Score Is 44 
  34.         2015-01-29 21:19:48.120 测试题10[2752:378621] 位置3的 Name Is s3 Age Is 33 Score Is 33 
  35.         2015-01-29 21:19:48.120 测试题10[2752:378621] 位置4的 Name Is s2 Age Is 22 Score Is 22 
  36.         2015-01-29 21:19:48.120 测试题10[2752:378621] 位置5的 Name Is s1 Age Is 11 Score Is 11 
  37.         */  
  38.         NSLog(@"排序结束");  
  39.           
  40.         [S1 release];  
  41.         [S2 release];  
  42.         [S3 release];  
  43.         [S4 release];  
  44.         [S5 release];  
  45.     }  
  46.     return 0;  
  47. }  

解析:在student.m 文件中我写了一个 内部函数 在接口声明部分是没有写的 只能在这个类中使用

这道题的难点在于排序 需自定义排序规则    

调用方式:

NSArray *sortarray=[array sortedArrayUsingSelector:@selector(comparestu:)];

其中comparestu 为自定义的内部方法 用于进行排序  返回结果为 NSComparisonResult

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值