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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ch8898163 中级黑马   /  2014-10-19 17:29  /  693 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. #import <Foundation/Foundation.h>

  2. typedef enum{
  3.     SexMan,
  4.     SexWoman,
  5.     SexUnkown

  6. } Sex;

  7. typedef struct{
  8.     int year;
  9.     int month;
  10.     int day;
  11. } Date;

  12. @interface Dog : NSObject
  13. {
  14.     @public
  15.     Sex SexMan;
  16.     Date _birthday;
  17.     int _weight;
  18.    
  19. }

  20. - (void)run;
  21. - (void)eat;
  22. @end

  23. @implementation Dog
  24. - (void)run
  25. {
  26.     _weight = _weight + 1;
  27.     NSLog(@"狗跑完这次后的体重是%d",_weight);
  28. }
  29. - (void)eat
  30. {
  31.     _weight = _weight - 1;
  32.     NSLog(@"狗跑完这次后的体重是%d",_weight);

  33. }
  34. @end

  35. int main ()
  36. {
  37.     Dog *d = [Dog new];
  38.    
  39.     d -> _weight = 19;
  40.     d -> _birthday.year = 2010;
  41.     d -> _birthday.month = 11;
  42.    
  43.     [d run];
  44.     [d eat];
  45.     NSLog(@"体重是%d,出生是%d-%d-%d的狗狗",_weight,_birthday.year,_birthday.month);
  46.    
  47.     return 0;
  48. }
复制代码


我没有截图软件,就给大家说一下,在最后输出的时候_weight使用了定义不明确的变量,报错,什么原因?

评分

参与人数 1技术分 +1 收起 理由
星河鹭起 + 1

查看全部评分

2 个回复

倒序浏览
呃,这个我自己想出来了,是因为函数里不能直接调用对象方法的变量~
回复 使用道具 举报
nice……洗洗睡了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马