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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© LuciferTJD 中级黑马   /  2015-4-9 14:39  /  966 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 汤靖咚 于 2015-4-9 21:28 编辑

求帮忙看看,这个最后应该输出的不是正在学习的是OC语言,为什么不是,结果提示没有书名,是哪个地方出错了呢?
  1. #import <Foundation/Foundation.h>

  2. // 人
  3. @interface Person : NSObject
  4. {
  5.     NSString *_name; // 姓名
  6.     int _age; // 年龄
  7. }
  8. // 姓名的setter和getter
  9. - (void)setName:(NSString *)name;
  10. - (NSString *)name;

  11. // 年龄的setter和getter
  12. - (void)setAge:(int)age;
  13. - (int)age;

  14. // 一种方法同时设置年龄和姓名
  15. - (void)setName:(NSString *)name andAge:(int)age;
  16. @end

  17. @implementation Person
  18. // 姓名的实现
  19. - (void)setName:(NSString *)name
  20. {
  21.     _name = name;
  22. }
  23. - (NSString *)name
  24. {
  25.     return _name;
  26. }

  27. // 年龄的实现
  28. - (void)setAge:(int)age
  29. {
  30.     _age = age;
  31. }
  32. - (int)age
  33. {
  34.     return _age;
  35. }

  36. // 同时设置姓名和年龄
  37. - (void)setName:(NSString *)name andAge:(int)age
  38. {
  39.     _name = name;
  40.     _age = age;
  41. }
  42. @end

  43. // 书
  44. @interface Book : NSObject
  45. {
  46.     NSString *_nameOfBook; // 书名
  47.     NSString *_nameOfPublisher; // 出版社
  48.     Person *_author; // 作者
  49. }
  50. // 书名getter和setter
  51. - (void)setNameOfBook:(NSString *)nameOfBook;
  52. - (NSString *)nameOfBook;


  53. // 出版社名称getter和setter
  54. - (void)setNameOfPublisher:(NSString *)nameOfPublisher;
  55. - (NSString *)nameOfPublisher;


  56. // 作者getter和setter
  57. - (void)setAuthor:(Person *)author;
  58. - (Person *)author;
  59. @end

  60. @implementation Book
  61. // 书名的实现
  62. - (void)setNameOfBook:(NSString *)nameOfBook
  63. {
  64.     _nameOfBook = nameOfBook;
  65. }
  66. - (NSString *)nameOfBook
  67. {
  68.     return _nameOfBook;
  69. }

  70. // 出版社名称的实现
  71. - (void)setNameOfPublisher:(NSString *)nameOfPublisher
  72. {
  73.     _nameOfPublisher = nameOfPublisher;
  74. }
  75. - (NSString *)nameOfPublisher
  76. {
  77.     return _nameOfPublisher;
  78. }

  79. // 作者的实现
  80. - (void)setAuthor:(Person *)author
  81. {
  82.     _author = author;
  83. }
  84. - (Person *)author
  85. {
  86.     return _author;
  87. }
  88. @end

  89. // 学生
  90. @interface Student : Person
  91. {
  92.     int _no;
  93.     Book *_book;
  94. }
  95. // 学号的getter和setter
  96. - (void)setNo:(int)no;
  97. - (int)no;

  98. // 书的getter和setter
  99. - (void)setBook:(Book *)book;
  100. - (Book *)book;

  101. // -study: 输出书名
  102. - (void)study;
  103. @end

  104. @implementation Student
  105. // 学号的实现
  106. - (void)setNo:(int)no
  107. {
  108.     _no = no;
  109. }
  110. - (int)no
  111. {
  112.     return _no;
  113. }

  114. // 书的实现
  115. - (void)setBook:(Book *)book
  116. {
  117.     _book = book;
  118. }
  119. - (Book *)book
  120. {
  121.     return _book;
  122. }

  123. // 学习
  124. - (void)study
  125. {
  126.     NSLog(@"正在学习的是%@", [_book nameOfBook]);
  127. }
  128. @end

  129. int main()
  130. {
  131.     // 创建学生对象
  132.     Student *stu = [Student new];
  133.     // 创建书对象
  134.     Book *b = [Book new];
  135.     // 调用书名setter方法,写入书名
  136.     [b setNameOfBook:@"OC语言"];
  137.     // 学生学习
  138.     [stu study];
  139.    
  140.     return 0;
  141. }
复制代码


继承输出.png (5.06 KB, 下载次数: 42)

继承输出.png

4 个回复

倒序浏览
为什么没有人回答呢
回复 使用道具 举报
本帖最后由 仰望的繁华 于 2015-4-9 21:01 编辑


添加第八行即可;
  1. int main()
  2. {
  3.     // 创建学生对象
  4.     Student *stu = [Student new];
  5.     // 创建书对象
  6.     Book *b = [Book new];
  7.     // 你得将创建的书对象,设置给此学生。然后这个学生才拥有这本书,后面的方法调用学生读的书才有名字啊。
  8.     stu.book = b;
  9.     // 调用书名setter方法,写入书名
  10.     [b setNameOfBook:@"OC语言"];
  11.     // 学生学习
  12.     [stu study];
  13.    
  14.     return 0;
  15. }
复制代码



回复 使用道具 举报
就是那么的
回复 使用道具 举报

非常感谢,刚看一直不知道怎么回事...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马