这个题目我在入学考试做过。
- #import <Foundation/Foundation.h>
- @interface Student : NSObject
- {
- NSString *_name; // 姓名
- int _age; // 年龄
- double _score; // 考试成绩
- }
- // _name的setter和getter
- - (void)setName:(NSString *)name;
- - (NSString *)name;
- // _age的setter和getter
- - (void)setAge:(int)age;
- - (int)age;
- // _score的setter和getter
- - (void)setScore:(double)score;
- - (double)score;
- // 不带参数的快速构造器
- + (Student *)student;
- // 带参数的快速构造器
- + (Student *)studentWithName:(NSString *)name andAge:(int)age andScore:(double)score;
- // 与其他Student对象比较大小的方法
- - (NSInteger)comperWithOtherStudent:(Student *)stu;
- @end
复制代码
- #import <Foundation/Foundation.h>
- #import "Student.h"
- int main(int argc, const char * argv[])
- {
- // 创建一个自动释放池
- @autoreleasepool
- {
- // 创建5个学生对象
- Student *stu1 = [Student studentWithName:@"jack" andAge:30 andScore:50];
- Student *stu2 = [Student studentWithName:@"lose" andAge:19 andScore:70];
- Student *stu3 = [Student studentWithName:@"anna" andAge:25 andScore:90];
- Student *stu4 = [Student studentWithName:@"fan" andAge:15 andScore:30];
- Student *stu5 = [Student studentWithName:@"tom" andAge:15 andScore:85];
-
- // 输出学生对象
- NSLog(@"%@", stu1);
-
- // 将5个学生对象添加到一个数组中
- NSArray *students = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, stu5, nil];
-
- // 对数组进行排序
- NSArray *array = [students sortedArrayUsingSelector:@selector(comperWithOtherStudent:)];
-
- // 输出排序后的数组
- NSLog(@"%@", array);
- }
-
- return 0;
- }
复制代码 |