黑马程序员技术交流社区

标题: 入学测试题10 [打印本页]

作者: 泥娃娃    时间: 2016-3-22 16:51
标题: 入学测试题10
测试题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
复制代码





作者: x_starry    时间: 2016-3-22 18:51
看着好牛犇犇啊,
作者: LiuLiu006    时间: 2016-4-2 23:57
看看...............................
作者: 最爱舒儿    时间: 2016-4-3 00:17
收藏了                    
作者: qq6937523    时间: 2016-4-3 07:32
先留着  看看  以后说不定就看的不懵逼了

作者: 堕落天使    时间: 2016-4-4 08:00
看着好复杂啊!
作者: 我是王觉瘦    时间: 2016-4-4 21:57
感谢楼主的分享
作者: 蜗牛的未来    时间: 2016-4-5 03:44
这是测试题的最后一题?
作者: UKnowINeedYou    时间: 2016-4-5 18:04
我也是这道    差不多吧  
/*

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;
}
作者: 又欠又欠    时间: 2016-4-5 18:20
好题 受用了
作者: a157051803    时间: 2016-4-5 21:37
感谢楼主的分享
作者: qhw_fight    时间: 2016-4-5 22:02
看着好牛X
作者: 缘之有缘    时间: 2016-4-5 22:10
厉害               
作者: iOSyinlixian    时间: 2016-4-5 23:17
牛哄哄的样子




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2