黑马程序员技术交流社区
标题: 入学测试题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
- #import <Foundation/Foundation.h>
- #import "Student.h"
- int main(int argc, const char * argv[]) {
- // 创建可以初始化成员变量的学生对象
- Student *stu1 = [[Student alloc ] initWithName:@"aa" andAge:18 andScore:90];
- Student *stu2 = [[Student alloc] initWithName:@"bb" andAge:19 andScore:92];
- Student *stu3 = [[Student alloc ]initWithName:@"cc" andAge:17 andScore:80];
- Student *stu4 = [[Student alloc ]initWithName:@"dd" andAge:20 andScore:66];
- Student *stu5 = [[Student alloc ]initWithName:@"ee" andAge:18 andScore:88];
-
- NSArray *stuArr = @[stu1,stu2,stu3,stu4,stu5];
- //创建字典,成绩按照从大到小的顺序排列
- NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO];
- //创建字典,年龄按从大到小顺序排列
- NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
- //创建字典,姓名按从小到大顺序排列
- NSSortDescriptor *sort3 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
- //对stuarr根据 sort1,2,3进行排序并赋值于stuendarr
-
- NSArray *stuArr2 = [stuArr sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1,sort2,sort3, nil]];
-
- // NSArray *stuendarr = [stuarray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort1, sort2,sort3, nil]];
- //遍历stuendarr
- [stuArr2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
- [obj printXinXi];
- }];
-
- [stu1 release];
- [stu2 release];
- [stu3 release];
- [stu4 release];
- [stu5 release];
- [sort1 release];
- [sort2 release];
- [sort3 release];
- [stuArr release];
- [stuArr2 release];
-
- return 0;
- }
复制代码
Student.h- #import <Foundation/Foundation.h>
- @interface Student : NSObject
- {
- NSString *_name;
- int _age;
- float _score;
- }
- //姓名的set/get方法声明
- -(void)setName:(NSString *)name;
- -(NSString *)name;
- //年龄的set/get方法声明
- -(void)setAge:(int)age;
- -(int)age;
- //考试成绩的set/get方法声明
- -(void)setScore:(float)score;
- -(float)score;
- //便利构造器
- -(instancetype)initWithName:(NSString *) name andAge:(int) age andScore:(float) score;
- //打印学生信息
- -(void)printXinXi;
- @end
复制代码
Student.m- #import "Student.h"
- @implementation Student
- -(void)dealloc{
-
- //先释放当前类自己的对象
- [_name release];
- NSLog(@"%@学生对象被回收",_name);
- //再释放父类的对象
- [super dealloc];
- }
- //姓名的set/get方法声明
- -(void)setName:(NSString *)name{
- //判断是否是新传入的对象,
- if (_name!=name) {
- // 先release旧值
- [_name release];//第一次是向nil发送release消息
- // 再retain新值
- _name = [name retain];
- }
-
- }
- -(NSString *)name{
- return _name;
- }
- //年龄的set/get方法声明
- -(void)setAge:(int)age{
- _age = age;
- }
- -(int)age{
- return _age;
- }
- //考试成绩的set/get方法声明
- -(void)setScore:(float)score{
- _score = score;
- }
- -(float)score{
- return _score;
- }
- //便利构造器
- -(instancetype)initWithName:(NSString *) name andAge:(int) age andScore:(float) score{
-
- self = [super init];
- if (self) {
- _name = name;
- _age = age;
- _score = score;
- }
- return self;
- }
- //打印学生信息
- -(void)printXinXi{
- NSLog(@"My Name Is %@ Age Is %d Score Is %.2f",_name,_age,_score);
- }
- @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 |