@fiy-fish
2015-07-15T12:20:26.000000Z
字数 3265
阅读 1448
Objective-c
// main.m// day05-05-课程类设计//// Created by Aaron on 15/7/7.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {@autoreleasepool {Student *std = [[Student alloc] initWithName:@"蜡笔小新" withAge:3];Course *course1 = [[Course alloc] init];[course1 setCourseName:@"IOS"];[course1 setTeacherName:@"小白"];[course1 setCourseTime:10];Course *course2 = [[Course alloc] init];[course2 setCourseName:@"Android"];[course2 setTeacherName:@"小黑"];[course2 setCourseTime:20];Course *course3 = [[Course alloc] init];[course3 setCourseName:@"winCE"];[course3 setTeacherName:@"小红"];[course3 setCourseTime:19];[std addCourse:course1];[std addCourse:course2];[std addCourse:course3];//[std showInfo];Course *c = [std getCourseByIndex:10];NSLog(@"%@",c);NSLog(@"%@",[std allTeacherName]);}return 0;}
#import <Foundation/Foundation.h>#import "Course.h"/*创建一个学生类,包含属性姓名name,年龄age,三门课程course1,course2,course3实现功能:初始化姓名和年龄设置某一门课程获取某一门课的对象获得学生所有任课老师的名字获得学生选课的所有课时获得学生选的所有课的课名打印学生的基本信息和课程信息*/@interface Student : NSObject{NSString *_name;NSInteger _age;NSMutableArray *_courseArray;}-(instancetype)initWithName:(NSString *)name withAge:(NSInteger)age;-(void)addCourse:(Course *)course;-(Course *)getCourseByIndex:(NSInteger)index;-(NSString *)allTeacherName;-(NSInteger)allTime;-(NSString *)allCourseName;-(void)showInfo;@end
import "Student.h"@implementation Student-(instancetype)initWithName:(NSString *)name withAge:(NSInteger)age{if(self = [super init]){_name = name;_age = age;}return self;}-(void)addCourse:(Course *)course{if(!_courseArray){_courseArray = [NSMutableArray array];}if([_courseArray count] < 3){[_courseArray addObject:course];}}-(Course *)getCourseByIndex:(NSInteger)index{if(index>=0 && index<3){return _courseArray[index];}return nil;}-(NSString *)allTeacherName{//return [self getStringByNum:0];return [self getStringBySelector:@selector(teacherName) andFormat:@"老师%d:%@ "];}-(NSString *)getStringBySelector:(SEL)sel andFormat:(NSString *)format{NSMutableString *str = [NSMutableString string];for(int i = 0; i < [_courseArray count]; i++){Course *obj = _courseArray[i];//[str appendFormat:format,i+1,//[obj performSelector:sel]];[str appendFormat:format,i+1,[obj performSelector:sel]];}return str;}#if 0-(NSString *)getStringByNum:(int)num{NSMutableString *str = [NSMutableString string];for(int i = 0; i < [_courseArray count]; i++){Course *obj = _courseArray[i];if(num == 0){[str appendFormat:@"老师%d:%@ ",i+1,[obj teacherName]];}else{[str appendFormat:@"课程%d:%@ ",i+1,[obj courseName]];}}return str;}#endif-(void)showInfo{NSLog(@"%@",_courseArray);}@end
#import <Foundation/Foundation.h>@interface Course : NSObject{NSString *_courseName;NSString *_teacherName;NSInteger _courseTime;}-(void)setCourseName:(NSString *)name;-(void)setTeacherName:(NSString *)name;-(void)setCourseTime:(NSInteger)time;-(NSString *)courseName;-(NSString *)teacherName;-(NSInteger)courseTime;@end
#import "Course.h"@implementation Course-(void)setCourseName:(NSString *)name{if(_courseName != name){_courseName = name;}}-(void)setTeacherName:(NSString *)name{if(_teacherName != name){_teacherName = name;}}-(void)setCourseTime:(NSInteger)time{if(_courseTime != time){_courseTime = time;}}-(NSString *)courseName{return _courseName;}-(NSString *)teacherName{return _teacherName;}-(NSInteger)courseTime{return _courseTime;}-(NSString *)description{return _courseName;}@end