//
// MyClass.h
// OC2_班级类
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Student.h" @interface MyClass : NSObject
{
NSString * _className;
NSMutableArray * _stuList;
} - (id)initWithClassName:(NSString *)name;
- (void)setClassName:(NSString *)name;
- (NSString*)className;
- (void)addStudent:(Student *)student;
- (void)addStudent:(Student *)student atIndex:(NSInteger) index;
- (void)removeStudent:(Student *)student;
- (void)removeStudentAtIndex:(NSInteger) index;
- (void)replaceStudent:(Student *)student atIndex:(NSInteger) index;
- (void)showStuList;
- (void)sortedByNumber;//按照学号升序
- (void)sortedByScore;//按照分数降序
- (void)sortedByName;//按照名字降序 + (void)testMyClass; @end
//
// MyClass.m
// OC2_班级类
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "MyClass.h" @implementation MyClass - (id)initWithClassName:(NSString *)name
{
if (self = [super init]) {
_className = name;
_stuList = [NSMutableArray array];
}
return self;
}
- (void)setClassName:(NSString *)name
{
_className = name;
} - (NSString*)className
{
return _className;
} - (void)addStudent:(Student *)student
{
if (![_stuList containsObject:student]) {
[_stuList addObject:student];
}
} - (void)addStudent:(Student *)student atIndex:(NSInteger) index
{
[_stuList insertObject:student atIndex:index];
} - (void)removeStudent:(Student *)student
{
[_stuList removeObject:student];
} - (void)removeStudentAtIndex:(NSInteger) index
{
[_stuList removeObjectAtIndex:index];
} - (void)replaceStudent:(Student *)student atIndex:(NSInteger) index
{
[_stuList replaceObjectAtIndex:index withObject:student];
} //遍历学生数组
- (void)showStuList
{
for (Student *stu in _stuList) {
[stu printStudent];
}
} - (void)sortedByNumber//按照学号升序
{
[_stuList sortUsingSelector:@selector(isSortByNum:)];
}
- (void)sortedByScore//按照分数降序
{
[_stuList sortUsingSelector:@selector(isSortByScore:)];
}
- (void)sortedByName//按照名字降序
{
[_stuList sortUsingSelector:@selector(isSortByName:)];
} + (void)testMyClass
{
MyClass *ios1509 = [[MyClass alloc] initWithClassName:@"ios1509"];
for (NSInteger i=; i<; i++) {
Student *stu = [[Student alloc] initWithName:[NSString stringWithFormat:@"name%d", arc4random()%+] number:arc4random()%+ score:arc4random()%];
[ios1509 addStudent:stu];
}
NSLog(@"排序前");
[ios1509 showStuList]; NSLog(@"分数排序后");
[ios1509 sortedByScore];
[ios1509 showStuList]; NSLog(@"学号排序后");
[ios1509 sortedByNumber];
[ios1509 showStuList]; NSLog(@"名字排序后");
[ios1509 sortedByName];
[ios1509 showStuList];
} @end
//
// Student.h
// OC2_班级类
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> @interface Student : NSObject
{
NSString * _name;
NSInteger _num;
NSInteger _score;
} - (id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score;
- (void)setName:(NSString *)name;
- (NSString *)name;
- (void)setNum:(NSInteger)num;
- (NSInteger)num;
- (void)setScore:(NSInteger)score;
- (NSInteger)score; - (BOOL)isSortByScore:(Student *)aStudent;
- (BOOL)isSortByNum:(Student *)aStudent;
- (NSComparisonResult)isSortByName:(Student *)aStudent; - (void)printStudent; @end
//
// Student.m
// OC2_班级类
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "Student.h" @implementation Student - (id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score
{
if(self = [super init])
{
_name = name;
_num = num;
_score = score;
}
return self;
}
- (void)setName:(NSString *)name
{
_name = name;
} - (NSString *)name
{
return _name;
} - (void)setNum:(NSInteger)num
{
_num = num;
} - (NSInteger)num
{
return _num;
} - (void)setScore:(NSInteger)score
{
_score = score;
} - (NSInteger)score
{
return _score;
} - (BOOL)isSortByScore:(Student *)aStudent
{
if ([self score]>[aStudent score]) {
return YES;
}
return NO;
} - (BOOL)isSortByNum:(Student *)aStudent
{
if ([self num] > [aStudent num]) {
return YES;
}
return NO;
} - (NSComparisonResult)isSortByName:(Student *)aStudent
{
return [[self name] compare:[aStudent name]];
} - (void)printStudent
{
NSLog(@"name = %@ num = %li score = %li", [self name], [self num], [self score]);
} @end
//
// main.m
// OC2_班级类
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "MyClass.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//NSLog(@"Hello, World!");
[MyClass testMyClass];
}
return ;
}