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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Melody丶Zhy 中级黑马   /  2015-1-31 23:04  /  2067 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

2黑马币
先上代码。。  这个身材中的数据用人的指针怎么调用啊
  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. }
复制代码

最佳答案

查看完整内容

身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);

评分

参与人数 1技术分 +1 收起 理由
黑妞~ + 1

查看全部评分

5 个回复

倒序浏览
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);
回复 使用道具 举报
恋梦 发表于 2015-1-31 23:33
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);

原来 对象的方法还用p.age访问 懂了 ~谢了
回复 使用道具 举报
好高端哦
                    自学中的菜鸟
回复 使用道具 举报
血玉漫天 发表于 2015-2-13 19:52
好高端哦
                    自学中的菜鸟

呃呃 我这个当时没看下面的视频就来问了。。也问了也看了下面的视频才发现能这样
回复 使用道具 举报
身材?? NSLog(@"年龄为%d,身高为%f,体重为%f",p.age, p.body.height, p.body.weight);
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马