黑马程序员技术交流社区

标题: 请问类的组合时候怎么对被组合类里面的属性进行赋值 [打印本页]

作者: chaoren    时间: 2015-3-12 03:26
标题: 请问类的组合时候怎么对被组合类里面的属性进行赋值

复制代码
对于student类继承了person类,组合了score类,请问我怎么对学生里面的score进行赋值?有没有大神指导下

作者: chaoren    时间: 2015-3-12 03:28
  1. #import <Foundation/Foundation.h>

  2. typedef struct Date{
  3.     int year;
  4.     int month;
  5.     int day
  6. };
  7. typedef enum  {
  8.     sexMan,
  9.     sexWoman
  10. }Sex;
  11. @interface Score : NSObject
  12. {
  13.     int _cScore;
  14.     int _ocScore;
  15.     int _iOSScore;
  16. }
  17. - (void)setCScore:(int)newCScore;
  18. - (int)cScore;
  19. - (int)ocScore;
  20. - (int)iOSScore;
  21. - (void)setOcScore:(int)newOcScore;
  22. - (void)setIosScore:(int)newIosScore;
  23. @end
  24. @implementation Score
  25. - (void)setCScore:(int)newCScore
  26. {
  27.     _cScore = newCScore;
  28. }
  29. - (int)cScore
  30. {
  31.     return _cScore;
  32. }
  33. - (int)ocScore
  34. {
  35.     return _ocScore;
  36. }
  37. - (int)iOSScore
  38. {
  39.     return _iOSScore;
  40. }
  41. - (void)setOcScore:(int)newOcScore
  42. {
  43.     _ocScore = newOcScore;
  44. }
  45. - (void)setIosScore:(int)newIosScore
  46. {
  47.     _iOSScore = newIosScore;
  48. }
  49. @end
  50. @interface Person: NSObject
  51. {
  52.     NSString *_name;//姓名
  53.     Date  *_birthday;//生日
  54.     int _age;//年龄
  55.     double _weight;//体重
  56.     double _height;//身高
  57.     Sex _mySex;//性别
  58. }
  59. - (void)setName:(NSString *)newName;
  60. - (NSString *)name;
  61. - (void)setBirthday:(Date)newBirthday;
  62. - (Date)birthday;
  63. - (int)age;
  64. - (void)setWeight:(double)newWeight;
  65. - (double)weight;
  66. - (void)setHeight:(double)newHeight;
  67. - (double)height;
  68. - (void)setSex:(Sex)newSex;
  69. - (Sex)sex;
  70. @end
  71. @implementation Person
  72. //set姓名
  73. - (void)setName:(NSString *)newName
  74. {
  75.     _name = newName;
  76. }
  77. //get姓名
  78. - (NSString *)name
  79. {
  80.     return _name;
  81. }
  82. //set生日
  83. - (void)setBirthday:(Date)newBirthday
  84. {
  85.     _birthday.year = newBirthday.year;
  86.     _birthday.month = newBirthday.month;
  87.     _birthday.day = newBirthday.day;
  88. }
  89. //get生日
  90. - (Date)birthday
  91. {
  92.     return _birthday;
  93. }
  94. //get年龄
  95. - (int)age
  96. {
  97.     return 2015 - _birthday.year;
  98. //    NSDate *date = [NSDate date];
  99. //    NSCalendar *cal = [NSCalendar currentCalendar];
  100. //    unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
  101. //    NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面;
  102. //    //然后就可以从d中获取具体的年月日了;
  103. //    NSInteger year = [d year];
  104. //    NSInteger month = [d month];
  105. //    NSInteger day  =  [d day];
  106. //   
  107. //    NSLog(@"%ld",year);
  108. //    NSLog(@"%ld",month);
  109. //    NSLog(@"%ld",day);
  110. //    _age =(int)year - _birthday.year;
  111. //    if (((int)month - _birthday.month)<0) {
  112. //        _age--;
  113. //    }else{
  114. //        if (<#condition#>) {
  115. //            <#statements#>
  116. //        }
  117. //    }
  118.    }
  119. //set体重
  120. - (void)setWeight:(double)newWeight
  121. {
  122.     _weight = newWeight;
  123. }
  124. //get体重
  125. - (double)weight
  126. {
  127.     return _weight;
  128. }
  129. //set身高
  130. - (void)setHeight:(double)newHeight
  131. {
  132.     _height = newHeight;
  133. }
  134. //get身高
  135. - (double)height
  136. {
  137.     return _height;
  138. }
  139. //set性别
  140. - (void)setSex:(Sex)newSex
  141. {
  142.     _mySex = newSex;
  143. }
  144. //get性别
  145. - (Sex)sex
  146. {
  147.     return _mySex;
  148. }
  149. @end

  150. @interface Student : Person
  151. {
  152.     Score  *_score;
  153. }
  154. - (void)setScore:(Score *)newScore;
  155. - (Score *)score;
  156. @end
  157. @implementation Student
  158. - (void)setScore:(Score *)newScore
  159. {
  160.     _score = newScore;
  161. }
  162. - (Score *)score
  163. {
  164.     return _score;
  165. }

  166. @end
  167. int main()
  168. {


  169.     return 0;
  170. }
复制代码

作者: chaoren    时间: 2015-3-12 03:29
相关代码如上,求指导
作者: chaoren    时间: 2015-3-12 13:36
  1. #import <Foundation/Foundation.h>

  2. //日期
  3. typedef struct {
  4.     int year;
  5.     int month;
  6.     int day;
  7. }Date;
  8. //性别枚举
  9. typedef enum  {
  10.     sexMan,
  11.     sexWoman
  12. }Sex;
  13. //score类申明
  14. @interface Score : NSObject
  15. {
  16.     int _cScore;
  17.     int _ocScore;
  18.     int _iOSScore;
  19. }
  20. - (void)setCScore:(int)newCScore;
  21. - (int)cScore;
  22. - (int)ocScore;
  23. - (int)iOSScore;
  24. - (void)setOcScore:(int)newOcScore;
  25. - (void)setIosScore:(int)newIosScore;
  26. @end
  27. //score方法实现
  28. @implementation Score
  29. - (void)setCScore:(int)newCScore
  30. {
  31.     _cScore = newCScore;
  32. }
  33. - (int)cScore
  34. {
  35.     return _cScore;
  36. }
  37. - (int)ocScore
  38. {
  39.     return _ocScore;
  40. }
  41. - (int)iOSScore
  42. {
  43.     return _iOSScore;
  44. }
  45. - (void)setOcScore:(int)newOcScore
  46. {
  47.     _ocScore = newOcScore;
  48. }
  49. - (void)setIosScore:(int)newIosScore
  50. {
  51.     _iOSScore = newIosScore;
  52. }
  53. @end
  54. //人类声明
  55. @interface Person: NSObject
  56. {
  57.     NSString *_name;//姓名
  58.     Date  _birthday;//生日
  59.     int _age;//年龄
  60.     double _weight;//体重
  61.     double _height;//身高
  62.     Sex _mySex;//性别
  63. }
  64. - (void)setName:(NSString *)newName;
  65. - (NSString *)name;
  66. - (void)setBirthday:(Date)newBirthday;
  67. - (Date)birthday;
  68. - (int)age;
  69. - (void)setWeight:(double)newWeight;
  70. - (double)weight;
  71. - (void)setHeight:(double)newHeight;
  72. - (double)height;
  73. - (void)setSex:(Sex)newSex;
  74. - (Sex)sex;
  75. @end
  76. //人类方法实现
  77. @implementation Person
  78. //set姓名
  79. - (void)setName:(NSString *)newName
  80. {
  81.     _name = newName;
  82. }
  83. //get姓名
  84. - (NSString *)name
  85. {
  86.     return _name;
  87. }
  88. //set生日
  89. - (void)setBirthday:(Date)newBirthday
  90. {
  91.     _birthday.year = newBirthday.year;
  92.     _birthday.month = newBirthday.month;
  93.     _birthday.day = newBirthday.day;
  94. }
  95. //get生日
  96. - (Date)birthday
  97. {
  98.     return _birthday;
  99. }
  100. //get年龄
  101. - (int)age
  102. {
  103.     return 2015 - _birthday.year;
  104. //    NSDate *date = [NSDate date];
  105. //    NSCalendar *cal = [NSCalendar currentCalendar];
  106. //    unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
  107. //    NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面;
  108. //    //然后就可以从d中获取具体的年月日了;
  109. //    NSInteger year = [d year];
  110. //    NSInteger month = [d month];
  111. //    NSInteger day  =  [d day];
  112. //   
  113. //    NSLog(@"%ld",year);
  114. //    NSLog(@"%ld",month);
  115. //    NSLog(@"%ld",day);
  116. //    _age =(int)year - _birthday.year;
  117. //    if (((int)month - _birthday.month)<0) {
  118. //        _age--;
  119. //    }else{
  120. //        if (<#condition#>) {
  121. //            <#statements#>
  122. //        }
  123. //    }
  124.    }
  125. //set体重
  126. - (void)setWeight:(double)newWeight
  127. {
  128.     _weight = newWeight;
  129. }
  130. //get体重
  131. - (double)weight
  132. {
  133.     return _weight;
  134. }
  135. //set身高
  136. - (void)setHeight:(double)newHeight
  137. {
  138.     _height = newHeight;
  139. }
  140. //get身高
  141. - (double)height
  142. {
  143.     return _height;
  144. }
  145. //set性别
  146. - (void)setSex:(Sex)newSex
  147. {
  148.     _mySex = newSex;
  149. }
  150. //get性别
  151. - (Sex)sex
  152. {
  153.     return _mySex;
  154. }
  155. @end
  156. //学生类:学生是人类,拥有成绩
  157. @interface Student : Person
  158. {
  159.     Score  *_score;//成绩。组合
  160. }
  161. - (void)setScore:(Score *)newScore;
  162. - (Score *)score;
  163. @end
  164. //学生类的方法实现
  165. @implementation Student
  166. - (void)setScore:(Score *)newScore
  167. {
  168.     _score = newScore;//给成绩赋值
  169. }
  170. - (Score *)score//返回成绩
  171. {
  172.     return _score;
  173. }

  174. @end
  175. int main()
  176. {
  177.     Student *stu = [Student new];
  178.     Score *sc = [Score new];
  179.     [stu setName:@"张三"];
  180.     [sc setCScore: 80];
  181.     [sc setOcScore: 90];
  182.     [sc setIosScore: 78];
  183.     [stu setScore:sc];
  184.     Score *newsc = [stu score];
  185.     NSLog(@"Cscore=%d,iosscore=%d,osScore=%d",[newsc cScore],[newsc iOSScore],[newsc ocScore]);
  186.     Date d = {1990,12,16};
  187.     [stu setBirthday:d];
  188.     NSLog(@"%@今年%d岁,他的的生日是%d-%d-%d",[stu name],[stu age],[stu birthday].year,[stu birthday].month,[stu birthday].day);

  189.     return 0;
  190. }
复制代码

已经实现了,不知道代码是否规范合乎逻辑




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2