20150612_OC之数组及复合类的练习

来源:互联网 发布:东莞知路电子有限公司 编辑:程序博客网 时间:2024/05/22 20:58

复合类的练习及数组的使用,其中使用到了两个比较方法:

////  Student.h//  IOS150611_ObjectiveC_StudentClass练习////  Created by Peng Junlong on 15/6/12.//  Copyright (c) 2015年 Peng Junlong. 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;//比较方法放在需要比较的对象的类中,而不是MyClass类中- (BOOL)isSortByName:(Student *)astudent;- (BOOL)isSortByScore:(Student *)astudent;- (BOOL)isSortByNum:(Student *)astudent;@end

////  Student.m//  IOS150611_ObjectiveC_StudentClass练习////  Created by Peng Junlong on 15/6/12.//  Copyright (c) 2015年 Peng Junlong. 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;}- (void)setNum:(NSInteger)num{    _num = num;}- (void)setScore:(NSInteger)score{    _score = score;}- (NSString *)name{    return _name;}- (NSInteger)num{    return _num;}- (NSInteger)score{    return _score;}- (BOOL)isSortByName:(Student *)astudent{    return [[astudent name] compare:[self name]];}- (BOOL)isSortByNum:(Student *)astudent{    if ([self num] > [astudent num]) {        return YES;    }    return NO;}- (BOOL)isSortByScore:(Student *)astudent{    if ([self score] < [astudent score]) {        return YES;    }    return NO;}@end

////  MyClass.h//  IOS150611_ObjectiveC_StudentClass练习////  Created by Peng Junlong on 15/6/12.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"@interface MyClass : NSObject{    NSString *_className;    NSMutableArray *_stuList;}- (id)init;- (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;       //降序@end

////  MyClass.m//  IOS150611_ObjectiveC_StudentClass练习////  Created by Peng Junlong on 15/6/12.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import "MyClass.h"@implementation MyClass- (id)init{    if (self = [super init]) {        _className = @"";        _stuList = [NSMutableArray arrayWithCapacity:20];    }    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{    NSLog(@"ClassName = %@",[self className]);    for (Student *stu in _stuList) {        NSLog(@"%@,%ld,%ld",[stu name],[stu num],[stu score]);    }}- (void)sortedByNumber{    //方法一//    [_stuList sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {//        return [obj1 num] > [obj2 num]; //  方法的实质是返回值为1的时候才进行交换,为0和-1不交换//    }];        //方法二    [_stuList sortUsingSelector:@selector(isSortByNum:)];}- (void)sortedByScore{    //方法一//    [_stuList sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {//        return [obj1 score] < [obj2 score];//    }];        //方法二    [_stuList sortUsingSelector:@selector(isSortByScore:)];}- (void)sortedByName{        //方法一//    [_stuList sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {//        NSString *str1=(NSString *)[obj1 name];//        NSString *str2=(NSString *)[obj2 name];//        return [str2 compare:str1];//    }];        //方法二    [_stuList sortUsingSelector:@selector(isSortByName:)];}@end

////  main.m//  IOS150611_ObjectiveC_StudentClass练习////  Created by Peng Junlong on 15/6/12.//  Copyright (c) 2015年 Peng Junlong. All rights reserved.//#import <Foundation/Foundation.h>#import "MyClass.h"int main(int argc, const char * argv[]) {    @autoreleasepool {               MyClass *myClass = [[MyClass alloc] init];        [myClass setClassName:@"IOS1509"];        Student *student1 = [[Student alloc] initWithName:@"atudent1" number:1234 score:88];        Student *student2 = [[Student alloc] initWithName:@"gtudent2" number:1235 score:89];        Student *student3 = [[Student alloc] initWithName:@"ttudent3" number:1236 score:87];        [myClass addStudent:student1];        [myClass addStudent:student3];        [myClass addStudent:student2 atIndex:0];        [myClass showStuList];        [myClass removeStudentAtIndex:1];        [myClass showStuList];                [myClass removeStudent:student2];        [myClass showStuList];                //[myClass addStudent:student1];        [myClass addStudent:student2 atIndex:1];        [myClass showStuList];                [myClass replaceStudent:student1 atIndex:0];        [myClass showStuList];                NSLog(@"=========按num升序===============");        [myClass sortedByNumber];        [myClass showStuList];        NSLog(@"=========按Score降序=============");        [myClass sortedByScore];        [myClass showStuList];        NSLog(@"=========按Name降序============");        [myClass sortedByName];        [myClass showStuList];                //=============产生一个随机数===========        NSInteger rand = arc4random();        NSLog(@"随机数:%ld",rand);            }    return 0;}


0 0
原创粉丝点击