黑马程序员技术交流社区

标题: 继承--类与类关联-super&&self [打印本页]

作者: YuePr    时间: 2015-12-7 21:23
标题: 继承--类与类关联-super&&self
  1. #import <Foundation/Foundation.h>
  2. @interface Dog : NSObject
  3. {
  4.     char *_name;
  5. }
  6. -(void)run;
  7. -(void)catchThief;
  8. @end
  9. @implementation Dog
  10. -(void)run
  11. {
  12.     NSLog(@"%s在跑",_name);
  13. }
  14. -(void)catchThief
  15. {
  16.     NSLog(@"警犬抓小偷");
  17. }
  18. @end

  19. @interface Police : NSObject
  20. {
  21.     Dog *_dog;
  22.     char *_name;
  23. }
  24. -(void)run;
  25. +(void)catchThief;
  26. -(void)setDog:(Dog *)dog;
  27. -(Dog *)getDog;
  28. @end
  29. @implementation Police
  30. -(void)run
  31. {
  32.     NSLog(@"%s在跑",_name);
  33. }
  34. +(void)catchThief
  35. {
  36.     NSLog(@"警察抓小偷");
  37. }
  38. -(void)setDog:(Dog *)dog
  39. {
  40.     _dog = dog;
  41. }
  42. -(Dog *)getDog
  43. {
  44.     return _dog;
  45. }
  46. @end

  47. int main()
  48. {
  49.     Police *jinPing = [Police new];
  50.     Dog *dog = [Dog new];
  51.     [jinPing setDog:dog];
  52.     [[jinPing getDog] catchThief];
  53.     return 0;
  54. }
复制代码

作者: 连桦懿    时间: 2015-12-7 21:31
写的不错啊
作者: 649685603    时间: 2015-12-7 21:40
不明所以




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