[关闭]
@fiy-fish 2015-07-15T20:20:26.000000Z 字数 3265 阅读 1269

day05-05-课程类设计

Objective-c


  1. // main.m
  2. // day05-05-课程类设计
  3. //
  4. // Created by Aaron on 15/7/7.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Student.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. Student *std = [[Student alloc] initWithName:@"蜡笔小新" withAge:3];
  12. Course *course1 = [[Course alloc] init];
  13. [course1 setCourseName:@"IOS"];
  14. [course1 setTeacherName:@"小白"];
  15. [course1 setCourseTime:10];
  16. Course *course2 = [[Course alloc] init];
  17. [course2 setCourseName:@"Android"];
  18. [course2 setTeacherName:@"小黑"];
  19. [course2 setCourseTime:20];
  20. Course *course3 = [[Course alloc] init];
  21. [course3 setCourseName:@"winCE"];
  22. [course3 setTeacherName:@"小红"];
  23. [course3 setCourseTime:19];
  24. [std addCourse:course1];
  25. [std addCourse:course2];
  26. [std addCourse:course3];
  27. //[std showInfo];
  28. Course *c = [std getCourseByIndex:10];
  29. NSLog(@"%@",c);
  30. NSLog(@"%@",[std allTeacherName]);
  31. }
  32. return 0;
  33. }

  1. #import <Foundation/Foundation.h>
  2. #import "Course.h"
  3. /*
  4. 创建一个学生类,包含属性
  5. 姓名name,年龄age,三门课程course1,course2,course3
  6. 实现功能:
  7. 初始化姓名和年龄
  8. 设置某一门课程
  9. 获取某一门课的对象
  10. 获得学生所有任课老师的名字
  11. 获得学生选课的所有课时
  12. 获得学生选的所有课的课名
  13. 打印学生的基本信息和课程信息
  14. */
  15. @interface Student : NSObject
  16. {
  17. NSString *_name;
  18. NSInteger _age;
  19. NSMutableArray *_courseArray;
  20. }
  21. -(instancetype)initWithName:(NSString *)name withAge:(NSInteger)age;
  22. -(void)addCourse:(Course *)course;
  23. -(Course *)getCourseByIndex:(NSInteger)index;
  24. -(NSString *)allTeacherName;
  25. -(NSInteger)allTime;
  26. -(NSString *)allCourseName;
  27. -(void)showInfo;
  28. @end

  1. import "Student.h"
  2. @implementation Student
  3. -(instancetype)initWithName:(NSString *)name withAge:(NSInteger)age
  4. {
  5. if(self = [super init])
  6. {
  7. _name = name;
  8. _age = age;
  9. }
  10. return self;
  11. }
  12. -(void)addCourse:(Course *)course
  13. {
  14. if(!_courseArray)
  15. {
  16. _courseArray = [NSMutableArray array];
  17. }
  18. if([_courseArray count] < 3)
  19. {
  20. [_courseArray addObject:course];
  21. }
  22. }
  23. -(Course *)getCourseByIndex:(NSInteger)index
  24. {
  25. if(index>=0 && index<3)
  26. {
  27. return _courseArray[index];
  28. }
  29. return nil;
  30. }
  31. -(NSString *)allTeacherName
  32. {
  33. //return [self getStringByNum:0];
  34. return [self getStringBySelector:@selector(teacherName) andFormat:@"老师%d:%@ "];
  35. }
  36. -(NSString *)getStringBySelector:(SEL)sel andFormat:(NSString *)format
  37. {
  38. NSMutableString *str = [NSMutableString string];
  39. for(int i = 0; i < [_courseArray count]; i++)
  40. {
  41. Course *obj = _courseArray[i];
  42. //[str appendFormat:format,i+1,
  43. //[obj performSelector:sel]];
  44. [str appendFormat:format,i+1,
  45. [obj performSelector:sel]];
  46. }
  47. return str;
  48. }
  49. #if 0
  50. -(NSString *)getStringByNum:(int)num
  51. {
  52. NSMutableString *str = [NSMutableString string];
  53. for(int i = 0; i < [_courseArray count]; i++)
  54. {
  55. Course *obj = _courseArray[i];
  56. if(num == 0)
  57. {
  58. [str appendFormat:@"老师%d:%@ ",i+1,
  59. [obj teacherName]];
  60. }
  61. else
  62. {
  63. [str appendFormat:@"课程%d:%@ ",i+1,
  64. [obj courseName]];
  65. }
  66. }
  67. return str;
  68. }
  69. #endif
  70. -(void)showInfo
  71. {
  72. NSLog(@"%@",_courseArray);
  73. }
  74. @end

  1. #import <Foundation/Foundation.h>
  2. @interface Course : NSObject
  3. {
  4. NSString *_courseName;
  5. NSString *_teacherName;
  6. NSInteger _courseTime;
  7. }
  8. -(void)setCourseName:(NSString *)name;
  9. -(void)setTeacherName:(NSString *)name;
  10. -(void)setCourseTime:(NSInteger)time;
  11. -(NSString *)courseName;
  12. -(NSString *)teacherName;
  13. -(NSInteger)courseTime;
  14. @end

  1. #import "Course.h"
  2. @implementation Course
  3. -(void)setCourseName:(NSString *)name
  4. {
  5. if(_courseName != name)
  6. {
  7. _courseName = name;
  8. }
  9. }
  10. -(void)setTeacherName:(NSString *)name
  11. {
  12. if(_teacherName != name)
  13. {
  14. _teacherName = name;
  15. }
  16. }
  17. -(void)setCourseTime:(NSInteger)time
  18. {
  19. if(_courseTime != time)
  20. {
  21. _courseTime = time;
  22. }
  23. }
  24. -(NSString *)courseName
  25. {
  26. return _courseName;
  27. }
  28. -(NSString *)teacherName
  29. {
  30. return _teacherName;
  31. }
  32. -(NSInteger)courseTime
  33. {
  34. return _courseTime;
  35. }
  36. -(NSString *)description
  37. {
  38. return _courseName;
  39. }
  40. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注