Objective-c语言_练习题3

来源:互联网 发布:python中文论坛 编辑:程序博客网 时间:2024/04/28 20:38

这是一道类设计题 主要考没有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个大小字母排序))

main.m


#import <Foundation/Foundation.h>

#import "Student.h"


int main(int argc,const char * argv[]) {

    @autoreleasepool {

        

        Student *student1=[Studentnew];

        student1.name=@"a1";

        student1.age=21;

        student1.score=47;

        

        Student *studnet2=[Studentnew];

        studnet2.name=@"j2";

        studnet2.age=64;

        studnet2.score=89;

        

        Student *student3=[Studentnew];

        student3.name=@"g3";

        student3.age=78;

        student3.score=36;

        

        Student *student4=[Studentnew];

        student4.name=@"o4";

        student4.age=29;

        student4.score=99;

        

        Student *student5=[Studentnew];

        student5.name=@"y5";

        student5.age=56;

        student5.score=84;

        

        

        NSArray *array=[NSArrayarrayWithObjects:student1,studnet2,student3,student4,student5,nil];

        

        NSArray *array2=[array sortedArrayUsingSelector:@selector(Sorted:)];

        //Sorted排序方法在Student里面

        

        //遍历数组

        [array2 enumerateObjectsUsingBlock:^(Student *stu,NSUInteger i, BOOL *stop) {

            NSLog(@"位置%lu Name Is %@ Age Is %lu Score Is %lu",i+1,stu.name,stu.age,stu.score);

        }];

  

    }

    return 0;

}

=======================================================================================
Student.h


#import <Foundation/Foundation.h>


@interface Student : NSObject

{

    NSString *_name;

    NSInteger _age;

    NSInteger _score;

}


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

-(NSString *)name;


-(void)setAge:(NSInteger)age;

-(NSInteger)age;


-(void)setScore:(NSInteger)score;

-(NSInteger)score;


-(void)printfStudent;


@end

======================================================
Student.m


#import "Student.h"


@implementation Student



-(void)setName:(NSString *)name

{

    _name=name;

}

-(NSString *)name

{

    return _name;

}


-(void)setAge:(NSInteger)age

{

    _age=age;

}

-(NSInteger)age

{

    return _age;

}


-(void)setScore:(NSInteger)score

{

    _score=score;

}

-(NSInteger)score

{

    return _score;

}


-(void)printfStudent

{

    NSLog(@"My Name is %@ Age is %ld Score is %ld",_name,_age,_score);

}



-(NSComparisonResult)Sorted:(Student *)studnet

{

    NSComparisonResult result=[[NSNumbernumberWithInteger:studnet.score]compare:[NSNumbernumberWithInteger:self.score]];

    if (result==NSOrderedSame)

    {

        result=[[NSNumbernumberWithInteger:studnet.age]compare:[NSNumbernumberWithInteger:self.age]];

        if (result==NSOrderedSame)

        {

            result=[studnet.name compare:self.name];

        }

    }

    return result;

}



@end





0 0