• OC班级类


    //
    //  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=0; i<10; i++) {
            Student *stu = [[Student alloc] initWithName:[NSString stringWithFormat:@"name%d", arc4random()%50+1] number:arc4random()%100+1 score:arc4random()%101];
            [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 0;
    }
  • 相关阅读:
    根据时间戳获取年月日时分秒
    除法函数,乘法函数,加法函数,减法函数
    禁止鼠标点右键
    获取cookie,设置cookie,删除cookie
    解决 堆栈 出现的父对象和子对象相关联的问题 (深拷贝)
    团队展示
    用户规格说明书——结对编程
    测试与优化——结对编程
    程序开发——结对编程
    程序开发初体验
  • 原文地址:https://www.cnblogs.com/0515offer/p/4572096.html
Copyright © 2020-2023  润新知