A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

xileo0

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© xileo0 初级黑马   /  2015-3-8 12:08  /  636 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  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. }
复制代码


1 个回复

倒序浏览
额。。问题不见了。要输出p对象的身材成员值,要怎样写输出语句,怎样调用组合的成员变量值
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马