Objective-c语言_练习题3
来源:互联网 发布:python中文论坛 编辑:程序博客网 时间:2024/04/28 20:38
这是一道类设计题 主要考没有ARC的情况下 手动写getter setter 手动进行内存管理。
定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建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[]) {
@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;
}
#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
#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
- Objective-c语言_练习题3
- Objective-c语言_练习题3
- Objective-c语言_数组练习题1
- Objective-c语言_数组练习题2:
- Objective-C语言_数组练习题
- Objective-c语言_复合
- Objective-c语言_类目
- Objective-c语言_延展
- Objective-c语言_协议
- Objective-c语言_计算机网络
- 1004: C语言考试练习题_排列
- C语言考试练习题_排列
- c语言练习题3
- C语言练习题(3)
- Objective-c语言_面向对象(封装)
- Objective-c语言_面向对象(继承)
- Objective-c语言_面向对象(多态)
- Objective-c语言_不可变字典
- c标签遍历map
- http随手记
- [从头学数学] 第24节 表内乘法(二)
- Ueditor文本编辑器图片上传配置
- ArcGIS应用分析--修建最小成本道路
- Objective-c语言_练习题3
- 如何计算时间复杂度
- android 图片操作之Xfermode
- Objective-C语言——内存管理1
- 枚举型数据用法
- 计算以2为底的log
- 博弈问题
- 单击dataGridView某一行时将dataGridView当前选择行的某列值赋值给某个文本框
- c标签遍历map