A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 泥娃娃 中级黑马   /  2016-3-22 16:51  /  1734 人查看  /  13 人回复  /   3 人收藏 转载请遵从CC协议 禁止商业使用本文

测试题10

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

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

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

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

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

main.m
  1. #import <Foundation/Foundation.h>
  2. #import "Student.h"
  3. int main(int argc, const char * argv[]) {
  4. //      创建可以初始化成员变量的学生对象
  5.         Student *stu1 = [[Student alloc ] initWithName:@"aa" andAge:18 andScore:90];
  6.         Student *stu2 = [[Student alloc] initWithName:@"bb" andAge:19 andScore:92];
  7.         Student *stu3 = [[Student alloc ]initWithName:@"cc" andAge:17 andScore:80];
  8.         Student *stu4 = [[Student alloc ]initWithName:@"dd" andAge:20 andScore:66];
  9.         Student *stu5 = [[Student alloc ]initWithName:@"ee" andAge:18 andScore:88];
  10.         
  11.         NSArray *stuArr = @[stu1,stu2,stu3,stu4,stu5];
  12.         //创建字典,成绩按照从大到小的顺序排列
  13.         NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
  14.         //创建字典,年龄按从大到小顺序排列
  15.         NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
  16.           //创建字典,姓名按从小到大顺序排列
  17.         NSSortDescriptor *sort3 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
  18.         //对stuarr根据 sort1,2,3进行排序并赋值于stuendarr
  19.         
  20.         NSArray *stuArr2 = [stuArr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1,sort2,sort3, nil]];
  21.         
  22. //        NSArray *stuendarr = [stuarray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1, sort2,sort3, nil]];
  23.         //遍历stuendarr
  24.         [stuArr2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
  25.             [obj printXinXi];
  26.         }];
  27.    
  28.     [stu1 release];
  29.     [stu2 release];
  30.     [stu3 release];
  31.     [stu4 release];
  32.     [stu5 release];
  33.     [sort1 release];
  34.     [sort2 release];
  35.     [sort3 release];
  36.     [stuArr release];
  37.     [stuArr2 release];
  38.    
  39.     return 0;
  40. }
复制代码

Student.h
  1. #import <Foundation/Foundation.h>

  2. @interface Student : NSObject
  3. {
  4.     NSString *_name;
  5.     int _age;
  6.     float _score;

  7. }
  8. //姓名的set/get方法声明
  9. -(void)setName:(NSString *)name;
  10. -(NSString *)name;
  11. //年龄的set/get方法声明
  12. -(void)setAge:(int)age;
  13. -(int)age;
  14. //考试成绩的set/get方法声明
  15. -(void)setScore:(float)score;
  16. -(float)score;


  17. //便利构造器

  18. -(instancetype)initWithName:(NSString *) name andAge:(int) age andScore:(float) score;


  19. //打印学生信息
  20. -(void)printXinXi;
  21. @end
复制代码

Student.m
  1. #import "Student.h"

  2. @implementation Student
  3. -(void)dealloc{
  4.    
  5.     //先释放当前类自己的对象
  6.     [_name release];
  7.     NSLog(@"%@学生对象被回收",_name);
  8.     //再释放父类的对象
  9.     [super dealloc];
  10. }
  11. //姓名的set/get方法声明
  12. -(void)setName:(NSString *)name{
  13.     //判断是否是新传入的对象,
  14.     if (_name!=name) {
  15.         // 先release旧值
  16.         [_name release];//第一次是向nil发送release消息
  17.         // 再retain新值
  18.         _name = [name retain];
  19.     }
  20.    
  21. }
  22. -(NSString *)name{
  23.     return _name;
  24. }
  25. //年龄的set/get方法声明
  26. -(void)setAge:(int)age{
  27.     _age = age;
  28. }

  29. -(int)age{
  30.     return _age;
  31. }

  32. //考试成绩的set/get方法声明
  33. -(void)setScore:(float)score{
  34.     _score = score;
  35. }

  36. -(float)score{
  37.     return _score;
  38. }

  39. //便利构造器

  40. -(instancetype)initWithName:(NSString *) name andAge:(int) age andScore:(float) score{
  41.    
  42.     self = [super init];
  43.     if (self) {
  44.         _name = name;
  45.         _age = age;
  46.         _score = score;
  47.     }
  48.     return self;

  49. }
  50. //打印学生信息
  51. -(void)printXinXi{

  52.     NSLog(@"My Name Is %@  Age Is %d Score Is %.2f",_name,_age,_score);
  53. }
  54. @end
复制代码




13 个回复

倒序浏览
看着好牛犇犇啊,
回复 使用道具 举报
看看...............................
回复 使用道具 举报
收藏了                    
回复 使用道具 举报
先留着  看看  以后说不定就看的不懵逼了
回复 使用道具 举报
看着好复杂啊!
回复 使用道具 举报
感谢楼主的分享
回复 使用道具 举报
这是测试题的最后一题?
回复 使用道具 举报
我也是这道    差不多吧  
/*

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

(Objective-C)

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

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

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

4)对5个学生对象按照成绩—》年龄—》姓名优先级排序(成绩相同按照年龄排序,成绩年龄相同按照姓名排
*/
#import <Foundation/Foundation.h>

@interface Student : NSObject{
       
        NSString *_name;
        int _age;
        float _score;
}
-(void)setName(NSString *) name;
-(NSString *)name;

-(void)setAge(int)age;
-(int)age;

-(void)setScore(float) score;
-(float)score;

-(instancetype)initWithName:(NSString *)name andAge:(int)age andScore:(float)score;

- (NSComparisonResult)comparestu:(Student *)stu;

+(instancetype)studentWithName:(NSString *)name andAge:(int)age andScore:(float)score;       

-(void)printInfo;

@end


@implementation Student

-(void)setName(NSString *) name{

        if(_name != name)    //这里要判断是否是原对象
        {
                [_name release];   //release旧值
               
                _name = [name retain];   //retain新的值
        }

}
-(NSString *)name{


        return _name;
}



-(void)setAge(int)age{

        _age = age;
}

-(int)age{

        return _age;
}

-(void)setScore(float) score{


        _score = score;
}
-(float)score{

        return _score;
}

-(void)dealloc{          //重写dealloc方法   

        [_name release];
        [super dealloc];

}


//快速构造器
-(instancetype)initWithName:(NSString *)name andAge:(int)age andScore:(float)score{

        if(self = [super init])   //先让父类要做的事做完
        {
                _name = name;
                _age = age;       
                _score = score;
               
        }

        return self;

}

+(instancetype)studentWithName:(NSString *)name andAge:(int)age andScore:(float)score{

        //快速创建对象,并且加到自动释放池中
        return [[[self alloc] initWithName:name andAge:age andScore:score] autorelease];


}


- (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;  
}  

-(void)printInfo{


        NSLog(@"My Name Is %@  Age Is %d Score Is %2.f",self.name,self.age,self.score);

}


@end



int main(int argc, const char * argv[])
{
    @autoreleasepool {
               
        Student *stu1 = [studentWithName:@"张三" andAge:23 andScore:88.5];
        Student *stu2 = [studentWithName:@"李四" andAge:21 andScore:89.5];
        Student *stu3 = [studentWithName:@"王五" andAge:22 andScore:91.0];
        Student *stu4 = [studentWithName:@"赵六" andAge:24 andScore:93.2];
        Student *stu5 = [studentWithName:@"龙七" andAge:20 andScore:81.5];

        [stu1 printInfo];
        [stu2 printInfo];
        [stu3 printInfo];
        [stu4 printInfo];
        [stu5 printInfo];

        NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,stu4,stu5,nil];

        NSArray *sortArray = [arr sortedArrayUsingSelector:@selector(comparestu)];
       


        //枚举法遍历数组并输出
            [sortArray enumerateObjectsUsingBlock:^(Student *stu, NSUInteger idx, BOOLBOOL *stop) {  
            NSLog(@"位置%i的 Name Is %@ Age Is %d Score Is %d",idx+1,stu.name,stu.age,stu.score);  
           }];
    }
   
    return 0;
}
回复 使用道具 举报
好题 受用了
回复 使用道具 举报
感谢楼主的分享
回复 使用道具 举报
看着好牛X
回复 使用道具 举报
厉害               
回复 使用道具 举报
牛哄哄的样子
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马