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

#import <Foundation/Foundation.h>

@interface Student : NSObject{
    NSString *_name;
    int _age;
    int _score;
}

-(void) setName:(NSString*) name;
-(NSString*) name;

-(void) setage:(int) age;
-(int)age;

-(void) setScore:(int) score;
-(int)score;

-(id) initwithname:(NSString*)name andage:(int)age andscore:(int)score;
-(void) printinfo;
+(id) stuwithname:(NSString*)name andage:(int)age andscore:(int)score;

@end
student.m

#import "Student.h"
@implementation Student
-(void)setName:(NSString *)name
{
    if (_name!=name) {
        [_name release];
        _name=[name retain];
    }
    _name=name;
}
-(NSString *)name{
    return _name;
}

-(void)setage:(int)age{
    _age=age;
}
-(int)age{
    return _age;
}

-(void)setScore:(int)score{
    _score=score;
}
-(int)score{
    return _score;
}

-(void)dealloc{
    
    [_name release];
    [super dealloc];
}

-(id)initwithname:(NSString *)name andage:(int)age andscore:(int)score{
    
    
    self=[super init];
    if (self!=nil) {
        _name=name;
        _age=age;
        _score=score;
    }
    return self;
}

+(id)stuwithname:(NSString *)name andage:(int)age andscore:(int)score{
    self=[[self alloc]initwithname:name andage:age andscore:score];
    return self;
}

-(void)printinfo{
    NSLog(@"My Name Is %@  Age Is %d Score Is %d",self.name,self.age,self.score);
}

- (NSComparisonResult)comparestu:(Student *)stu
{
    //先按分数
    
    NSComparisonResult result = [[NSNumber numberWithInt:stu.score]compare:[NSNumber numberWithInt:self.score]];
    //如果分数相同 按年龄
    if (result == NSOrderedSame) {
        result = [[NSNumber numberWithInt:stu.age]compare:[NSNumber numberWithInt:self.age]];
        if(result == NSOrderedSame) // 如果年龄一样,就按姓名排序
        {
            //如果年龄相同按姓名
            result = [self.name compare:stu.name];
        }
    }
    return result;
}
@end
main.m

#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *S1=[Student stuwithname:@"s1" andage:11 andscore:11];
        Student *S2=[Student stuwithname:@"s2" andage:22 andscore:22];
        Student *S3=[Student stuwithname:@"s3" andage:33 andscore:33];
        Student *S4=[Student stuwithname:@"s4" andage:44 andscore:44];
        Student *S5=[Student stuwithname:@"s5" andage:55 andscore:55];

        [S1 printinfo];
        [S2 printinfo];
        [S3 printinfo];
        [S4 printinfo];
        [S5 printinfo];

        NSArray *array = [NSArray arrayWithObjects:S1,S2,S3,S4,S5, nil];
        

        NSLog(@"开始排序");
        NSArray *sortarray=[array sortedArrayUsingSelector:@selector(comparestu:)];
        
        //NSLog(@"%@",sortarray);
        
        //遍历数组
        [sortarray enumerateObjectsUsingBlock:^(Student *stu, NSUInteger idx, BOOL *stop) {
            NSLog(@"位置%i的 Name Is %@ Age Is %d Score Is %d",idx+1,stu.name,stu.age,stu.score);
           }];
        
        /*
         输出结果
        2015-01-29 21:19:48.120 测试题10[2752:378621] 位置1的 Name Is s5 Age Is 55 Score Is 55
        2015-01-29 21:19:48.120 测试题10[2752:378621] 位置2的 Name Is s4 Age Is 44 Score Is 44
        2015-01-29 21:19:48.120 测试题10[2752:378621] 位置3的 Name Is s3 Age Is 33 Score Is 33
        2015-01-29 21:19:48.120 测试题10[2752:378621] 位置4的 Name Is s2 Age Is 22 Score Is 22
        2015-01-29 21:19:48.120 测试题10[2752:378621] 位置5的 Name Is s1 Age Is 11 Score Is 11
        */
        NSLog(@"排序结束");
        
        [S1 release];
        [S2 release];
        [S3 release];
        [S4 release];
        [S5 release];
    }
    return 0;
}

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

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

调用方式:

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

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

过两天我会单独写一个关于NSArray 排序相关的博文。 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值