测试题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
复制代码
|