黑马程序员技术交流社区

标题: OC组合之后怎么调用的问题 [打印本页]

作者: Melody丶Zhy    时间: 2015-1-31 23:04
标题: OC组合之后怎么调用的问题
先上代码。。  这个身材中的数据用人的指针怎么调用啊
  1. #import <Foundation/Foundation.h>

  2. @interface Body : NSObject
  3. {
  4.     int _height;
  5.     double _weight;
  6.     int _shouChang;
  7.     int _jiaoChang;
  8. }

  9. - (void)setHeight:(int)height;
  10. - (int)height;

  11. - (void)setWeight:(int)weight;
  12. - (int)weight;

  13. - (void)setShouChang:(int)shouChang;
  14. - (int)shouChang;

  15. - (void)setJiaoChang:(int)jiaoChang;
  16. - (int)jiaoChang;
  17. @end

  18. @implementation Body

  19. - (void)setHeight:(int)height
  20. {
  21.     if(height <20){
  22.         height = 20;
  23.     }
  24.     _height = height;
  25. }
  26. - (int)height
  27. {
  28.     return _height;
  29. }

  30. - (void)setWeight:(int)weight
  31. {
  32.     if(weight < 10){
  33.         weight = 10;
  34.     }
  35.     _weight = weight;
  36. }
  37. - (int)weight
  38. {
  39.     return _weight;
  40. }

  41. - (void)setShouChang:(int)shouChang
  42. {
  43.     if (shouChang < 5) {
  44.         shouChang = 5;
  45.     }
  46.     _shouChang = shouChang;
  47. }
  48. - (int)shouChang
  49. {
  50.     return _shouChang;
  51. }

  52. - (void)setJiaoChang:(int)jiaoChang
  53. {
  54.     if (jiaoChang < 6) {
  55.         jiaoChang = 6;
  56.     }
  57.     _jiaoChang = jiaoChang;
  58. }
  59. - (int)jiaoChang
  60. {
  61.     return _jiaoChang;
  62. }
  63. @end

  64. @interface Person : NSObject
  65. {
  66.     Body *_body;
  67.     int _age;
  68. }

  69. - (void)setAge:(int)age;
  70. - (int)age;

  71. - (void)setBody:(Body *)body;
  72. - (Body *)body;
  73. @end

  74. @implementation Person

  75. - (void)setAge:(int)age
  76. {
  77.     if (age <= 0) {
  78.         age = 1;
  79.     }
  80.     _age = age;
  81. }
  82. - (int)age
  83. {
  84.     return _age;
  85. }

  86. - (void)setBody:(Body *)body
  87. {
  88.     _body = body;
  89. }
  90. - (Body *)body
  91. {
  92.     return _body;
  93. }
  94. @end

  95. int main()
  96. {
  97.    Person *p = [Person new];
  98.    
  99.     [p setAge:10];
  100.    
  101.    Body *b = [Body new];
  102.     [b setHeight:178];
  103.     [b setWeight:63];
  104.    
  105.     [p setBody:b];
  106.     NSLog(@"年龄为:%d的人身材为???", [p age],?????);
  107.    
  108.     return 0;
  109. }
复制代码


作者: 恋梦    时间: 2015-1-31 23:04
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);
作者: Melody丶Zhy    时间: 2015-2-1 13:35
恋梦 发表于 2015-1-31 23:33
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);

原来 对象的方法还用p.age访问 懂了 ~谢了
作者: 血玉漫天    时间: 2015-2-13 19:52
好高端哦
                    自学中的菜鸟
作者: Melody丶Zhy    时间: 2015-2-13 22:37
血玉漫天 发表于 2015-2-13 19:52
好高端哦
                    自学中的菜鸟

呃呃 我这个当时没看下面的视频就来问了。。也问了也看了下面的视频才发现能这样
作者: 1115302782    时间: 2015-7-1 22:03
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);




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