1. 设计要求见.h文件。
Student.h
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
#import
@interface Student : NSObject
{
NSString *_name;
NSInteger _num;
NSInteger _score;
}
//构造方法
-(id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score;
//setter
-(void)setName:(NSString *)name;
-(void)setNum:(NSInteger)num;
-(void)setScore:(NSInteger)score;
//getter
-(NSString *)name;
-(NSInteger)num;
-(NSInteger)score;
//功能方法
-(BOOL)isSortByScore:(Student *)aStudent;
-(bool)isSortByNum:(Student *)aStudent;
-(NSComparisonResult)isSortByName:(Student *)aStudent;
-(void)printStudnent;
@end
MyClass.h
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
#import
#import "Student.h"
@interface MyClass : NSObject
{
NSString *_className;
NSMutableArray *_stuList;
}
-(id)initWithClassName:(NSString *)name;
-(void)setClassName:(NSString *)name;
-(NSString *)className;
-(void)addStudent:(Student *)astudent;
-(void)addStudent:(Student *)astudent atIndex:(NSInteger)index;
-(void)removeStudent:(Student *)astudent;
-(void)removeStudentAtIndex:(NSInteger)index;
-(void)replaceStudent:(Student *)astudent atIndex:(NSInteger)index;
-(void)showStuList;
-(void)sortedByNumber;
-(void)sortedByScore;
-(void)sortedByName;
+(void)testMyClass;
@end
2. .m实现文件(有问题)
Student.m
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s9 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
#import "Student.h"
@implementation Student
//构造方法
-(id)initWithName:(NSString *)name number:(NSInteger)num score:(NSInteger)score
{
self = [super self];
if (self) {
_name=name;
_num=num;
_score=score;
}
return self;
}
//setter
-(void)setName:(NSString *)name
{
_name=name;
}
-(void)setNum:(NSInteger)num
{
_num=num;
}
-(void)setScore:(NSInteger)score
{
_score=score;
}
//getter
-(NSString *)name
{
return _name;
}
-(NSInteger)num
{
return _num;
}
-(NSInteger)score
{
return _score;
}
//功能方法
-(BOOL)isSortByScore:(Student *)aStudent
{
if ([self score]>[aStudent score]) { //调用者分数>参数分数
return YES;
}
else
return NO;
}
-(bool)isSortByNum:(Student *)aStudent
{
if ([self num]>[aStudent num]) {
return YES;
}
else
return NO;
}
-(NSComparisonResult)isSortByName:(Student *)aStudent
{
return [[self name] compare:[aStudent name]];
}
-(void)printStudnent
{
NSLog(@"name=%@, num=%li, score=%li",[self name],[self num],[self score]);
}
@end
MyClass.m
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s11 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s12 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s13 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s14 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s15 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
#import "MyClass.h"
@implementation MyClass
-(id)initWithClassName:(NSString *)name
{
self=[super self];
if (self) {
_className=name;
}
return self;
}
-(void)setClassName:(NSString *)name
{
_className=name;
}
-(NSString *)className
{
return _className;
}
-(void)addStudent:(Student *)astudent
{
if (![_stuList containsObject:astudent]) {
[_stuList addObject:astudent];
}
}
-(void)addStudent:(Student *)astudent atIndex:(NSInteger)index
{
if (![_stuList containsObject:astudent]) {
[_stuList insertObject:astudent atIndex:index];
}
}
-(void)removeStudent:(Student *)astudent
{
if ([_stuList containsObject:astudent]) {
[_stuList removeObject:astudent];
}
}
-(void)removeStudentAtIndex:(NSInteger)index
{
[_stuList removeObjectAtIndex:index];
}
-(void)replaceStudent:(Student *)astudent atIndex:(NSInteger)index
{
[_stuList replaceObjectAtIndex:index withObject:astudent];
}
-(void)showStuList //遍历可变数组
{
NSLog(@"111");
NSLog(@"学生列表:%@",_stuList);
Student *obj;
for(obj in _stuList)
{
[obj printStudnent];
}
}
-(void)sortedByNumber //降序排列
{
[_stuList sortUsingSelector:@selector(isSortByNum:)];
}
-(void)sortedByScore
{
[_stuList sortUsingSelector:@selector(isSortByScore:)];
}
-(void)sortedByName
{
[_stuList sortUsingSelector:@selector(isSortByName:)];
}
//类方法
+(void)testMyClass
{
MyClass *iosClass=[[MyClass alloc]initWithClassName:@"ios班级"];
for (NSInteger i=0; i<10; i++) {
Student *stu = [[Student alloc]initWithName:[NSString stringWithFormat:@"stu%d",arc4random()%50+1] number:arc4random()%100+1 score:arc4random()%101];
[iosClass addStudent:stu];
}
NSLog(@"---排序前----");
[iosClass showStuList];
}
@end
main函数
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
int main(int argc, const char * argv[]) {
@autoreleasepool {
[MyClass testMyClass];
}
return 0;
}
一开始直接调用[MyClass testMyClass],没有打印出排序前的课程信息。在几个关键语句前加了测试输出函数,发现问题出在MyClass类中的addStudent方法。可以成功调用并运行这个函数,但是运行结束后学生列表仍为空。
测试输出结果如下图:
3. 代码修改
针对之前的问题,浏览了一遍代码,首先发现了两个低级错误,就是在构造函数中把 self = [super init] 错写成了 self = [super self],修改之后输出结果没变化。
因为输出结果是学生列表指针为空,所以我首先想到的是Class类中添加学生到列表的函数出错
(void)addStudent:(Student *)astudent
仔细检查并重写了个简单的test函数,发现这个函数没错,我就开始检查学生列表 _stuList的初始化问题。发现在Class构造函数中没有初始化这个列表,所以指针一直为空。
所以将MyClass.m中的构造函数修改为:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}
-(id)initWithClassName:(NSString *)name
{
self=[super init];
if (self) {
_className=name;
//创建可变数组
_stuList= [NSMutableArray array];
}
return self;
}
再次运行,结果正确。
之后在test函数加入测试排序功能:
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}
NSLog(@"---排序前----");
[iosClass showStuList];
NSLog(@"---name降序排序----");
[iosClass sortedByName];
[iosClass showStuList];
NSLog(@"---number升序排序----");
[iosClass sortedByNumber];
[iosClass showStuList];
NSLog(@"---score升序排序----");
[iosClass sortedByScore];
[iosClass showStuList];
得到正确结果: