黑马程序员技术交流社区

标题: 继承和组合,对象调用 [打印本页]

作者: xileo0    时间: 2015-3-8 12:08
标题: 继承和组合,对象调用

  1. #import <Foundation/Foundation.h>
  2. //身材
  3. @interface Stature : NSObject
  4. {
  5.     double _height;//身高 /m
  6.     double _weight;//体重 /kg
  7. }
  8. - (void)setHeight:(double)height;
  9. - (double)height;
  10. - (void)setWeight:(double)weight;
  11. - (double)weight;
  12. @end
  13. @implementation Stature
  14. // 身高的getter和setter
  15. - (void)setHeight:(double)height
  16. {
  17.     _height = height;
  18. }
  19. - (double)height
  20. {
  21.     return _height;
  22. }
  23. // 体重的getter和setter
  24. - (void)setWeight:(double)weight
  25. {
  26.     _weight = weight;
  27. }
  28. - (double)weight
  29. {
  30.     return _weight;
  31. }
  32. @end

  33. @interface Person : NSObject
  34. {
  35.     Stature *_stature;//身材
  36.     int _age;//年龄
  37. }
  38. - (void)setAge:(int)age;
  39. - (int)age;
  40. //---身材的setter和getter
  41. - (void)setStature:(Stature *)stature;
  42. - (Stature *)stature;
  43. @end
  44. @implementation Person
  45. - (void)setAge:(int)age
  46. {
  47.     _age = age;
  48. }
  49. - (int)age
  50. {
  51.     return _age;
  52. }
  53. - (void)setStature:(Stature *)stature
  54. {
  55.     _stature = stature;
  56. }
  57. - (Stature *)stature
  58. {
  59.     return _stature;
  60. }
  61. @end
  62. int main()
  63. {
  64.     Person *p = [Person new];
  65.     [p setAge:24];//p.age
  66.     Stature *s = [Stature new];
  67.     [s setHeight:1.8];
  68.     [s setWeight:60];
  69.    
  70.     [p setStature:s];
  71.     return 0;
  72. }
复制代码



作者: xileo0    时间: 2015-3-8 12:09
额。。问题不见了。要输出p对象的身材成员值,要怎样写输出语句,怎样调用组合的成员变量值




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