定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C) //1) 不使用@property,手动编写他们的访问器方法(getter和setter),注意内存管理(手动管理内存) //2) 增加一个便利构造器(快速构造器) //3) 使用NSLog输出学生对象时,输出信息格式为:My Name Is XXX Age Is XXX Score Is XXX //4) 对5个学生对象按照成绩—》年龄—》姓名优先级排序(成绩相同按照年龄排序,成绩年龄相同按照姓名排序(5个学生的属性值自己随便设定,姓名不考虑中文,按26个大小字母排序)) //////////////Student.h////////////////////////// #import <Foundation/Foundation.h>
@interface Student : NSObject { NSString * _name; int _age; int _score; }
-(void)setName:(NSString *)name; -(NSString *)name;
-(void)setAge:(int )age; -(int)age;
-(void)setScore:(int)score; -(int)score; -(instancetype)initWithName:(NSString *)name andAge:(int)age andScore:(int)score; -(void)print; +(BOOL)sort:(Student*) stuA and:(Student*)stuB; +(BOOL)sortName:(NSString *)nameA and:(NSString *)nameB; @end
//////////////Student.m////////////////////////////// #import "Student.h"
@implementation Student
-(void)setName:(NSString *)name{ _name = name; } -(NSString *)name{ return _name; }
-(void)setAge:(int )age{ _age = age; } -(int)age{ return _age; }
-(void)setScore:(int)score{ _score = score; } -(int)score{ return _score; }
- (void)dealloc { NSLog(@"student dealloc,%@",_name); [_name release]; [super dealloc];
}
-(instancetype)initWithName:(NSString *)name andAge:(int)age andScore:(int)score{ if (self = [super init]) { _name = name; _age = age; _score = score; } return self; }
-(void)print{ NSLog(@"My Name Is %@ Age Is %d Score Is %d",_name,_age,_score); }
+(BOOL)sort:(Student*) stuA and:(Student*)stuB{ if ([stuA score] > [stuB score]) { return YES; }else if ([stuA score] < [stuB score]){ return NO; }else { if ([stuA age] < [stuB age]) { return YES; }else if ([stuA age] > [stuB age]){ return NO; }else{ NSString* nameA = [[stuA name] lowercaseString]; NSString* nameB = [[stuB name] lowercaseString]; if ([self sortName:nameA and:nameB]){ return YES; }else{ return NO; } } } } +(BOOL)sortName:(NSString *)nameA and:(NSString *)nameB{ unsigned long time; if ([nameA length]>[nameB length]) { time = [nameB length]; }else{ time = [nameA length]; } for (int i = 0; i<time; i++) { if ([nameA characterAtIndex:i]<[nameB characterAtIndex:i]) { return YES; }else if ([nameA characterAtIndex:i]>[nameB characterAtIndex:i]){ return NO; } } return [nameA length]<[nameB length]; } @end //////////////main.m////////////////////////////////// #import <Foundation/Foundation.h> #import "Student.h"
int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu1 = [[Student alloc] initWithName:@"zhang" andAge:14 andScore:67]; Student *stu2 = [[Student alloc] initWithName:@"lissa" andAge:15 andScore:89]; Student *stu3 = [[Student alloc] initWithName:@"wangwu" andAge:34 andScore:89]; Student *stu4 = [[Student alloc] initWithName:@"hidemi" andAge:23 andScore:34]; Student *stu5 = [[Student alloc] initWithName:@"saela" andAge:34 andScore:97]; //NSMutableArray * arr =[ NSMutableArray arrayWithCapacity:5]; NSMutableArray * arr =[ NSMutableArray new]; [arr addObject:stu1]; [arr addObject:stu2]; [arr addObject:stu3]; [arr addObject:stu4]; [arr addObject:stu5];
NSUInteger count = [arr count]; Student * s = [Student new]; for (int i = 0; i<count; i++) { for (int j = 0; j<count; j++) { if (j<count-1 && ![Student sort:arr[j] and:arr[j+1]]) { s =arr[j]; [arr setObject:arr[j+1] atIndexedSubscript:j]; [arr setObject:s atIndexedSubscript:j+1]; } } } // [s release]; //->也许,是在赋值的时候付给了s一个指针,在release的时候释放掉一遍之后不能释放第二遍
for (int i = 0; i<5; i++) { [[arr objectAtIndex:i] print]; }
[stu5 release]; [stu4 release]; [stu3 release]; [stu2 release]; [stu1 release]; [arr removeAllObjects]; [arr release]; } return 0; }
那么问题来了,,,标记的这一句[s release],该怎么释放内存才不会出错。 如果按照我的代码中的方式,在释放某个对象的时候一定是会报错的,我考虑了一下就是因为,那个对象的指针付给了s,在释放s的内存的时候把原本的对象的空间释放掉了。所以在释放原本对象的时候会报错。
|