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个大小字母排序))
Student.h文件
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *name;
int age;
float score;
}
-(void)setName:(NSString *)newname;
-(NSString *)name;
-(void)setAge:(int)newage;
-(int)age;
-(void)setScore:(float)newscore;
-(float)score;
-(Student *)initWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore;
+(Student *)studentWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore;
-(NSComparisonResult)compareWithScore:(Student *)stu;
-(NSComparisonResult)compareWithAge:(Student *)stu;
-(NSComparisonResult)compareWithName:(Student *)stu;
@end
Student.m文件
#import "Student.h"
@implementation Student
-(void)setName:(NSString *)newname{
name=newname;
}
-(NSString *)name{
return name;
}
-(void)setAge:(int)newage{
age=newage;
}
-(int)age{
return age;
}
-(void)setScore:(float)newscore{
score=newscore;
}
-(float)score{
return score;
}
-(Student *)initWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore{
self=[super init];
if (self) {
name=newname;
age=newage;
score=newscore;
}
return self;
}
+(Student *)studentWithName:(NSString *)newname andAge:(int)newage andScore:(float)newscore{
Student *stu=[[Student alloc]initWithName:newname andAge:newage andScore:newscore];
return stu;
}
-(NSComparisonResult)compareWithScore:(Student *)stu{
NSComparisonResult result1;
if (self.score>stu.score) {
return NSOrderedDescending;
}
else if (self.score<stu.score){
return NSOrderedAscending;
}
else{
return NSOrderedSame;
}
return result1;
}
-(NSComparisonResult)compareWithAge:(Student *)stu{
NSComparisonResult result2;
if (self.age>stu.age) {
return NSOrderedDescending;
} else if(self.age<stu.age){
return NSOrderedAscending;
}
else{
return NSOrderedSame;
}
return result2;
}
-(NSComparisonResult)compareWithName:(Student *)stu{
return [self.name compare:stu.name];
}
-(NSString *)description{
NSString *str=[NSString stringWithFormat:@"%f,%d,%@",score,age,name];
return str;
}
@end
测试代码
Student *stu=[Student new];
[stu setScore:65.5];
[stu setAge:21];
[stu setName:@"zhangsan"];
Student *stu1=[Student new];
[stu1 setScore:70.3];
[stu1 setAge:19];
[stu1 setName:@"lisi"];
Student *stu2=[Student new];
[stu2 setScore:56.5];
[stu2 setAge:18];
[stu2 setName:@"wangwu"];
Student *stu3=[Student new];
[stu3 setScore:85.6];
[stu3 setAge:25];
[stu3 setName:@"mingming"];
Student *stu4=[Student new];
[stu4 setScore:95.4];
[stu4 setAge:20];
[stu4 setName:@"xionghong"];
NSArray *stuArr=[NSArray arrayWithObjects:stu,stu1,stu2,stu3,stu4, nil];
NSArray *sortArr=[stuArr sortedArrayUsingSelector:@selector(compareWithScore:)];
NSLog(@"按成绩排序后:");
for (int i=0; i<sortArr.count; i++) {
Student *a=[sortArr objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
NSArray *sortArr1=[stuArr sortedArrayUsingSelector:@selector(compareWithAge:)];
NSLog(@"按年龄排序后:");{
for (int i=0; i<sortArr1.count; i++) {
Student *a=[sortArr1 objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
}
NSArray *sortArr2=[stuArr sortedArrayUsingSelector:@selector(compareWithName:)];
NSLog(@"按姓名排序后:");{
for (int i=0; i<sortArr2.count; i++) {
Student *a=[sortArr2 objectAtIndex:i];
NSLog(@"Myname is %@,age is %d,score is %f",a.name,a.age,a.score);
}
}