本帖最后由 执着的土豆 于 2014-4-27 15:43 编辑
大家帮忙看看,为什么这样调用会报错呢?直接上代码和运行结果吧。
- #import <Foundation/Foundation.h>
- @interface Penson :NSObject
- {
- int _age;
- }
- - (void)setAge:(int)age;
- - (int)age;
- + (void)setAge:(int)age;
- + (int)age;
- @end
-
- @implementation Penson
- - (void)setAge:(int)age
- {
- _age=age;
- }
- -(int)age
- {
- return _age;
- }
- + (void)setAge:(int)age
- {
- _age=age;
- NSLog(@"%d",_age);
- }
- + (int)age
- {
- [self setAge:20];
- return _age;
- }
- @end
- int main()
- {
- Penson *p= [Penson new];
- [p setAge:10 ];
- NSLog(@"%d",[p age]);
- NSLog(@"%d",[Penson age] );
- return 0;
-
- }
复制代码
运行结果:
3三大特性的练习.m:23:4: error: instance variable '_age' accessed in class method _age=age; ^ 3三大特性的练习.m:24:17: error: instance variable '_age' accessed in class method NSLog(@"%d",_age); ^ 3三大特性的练习.m:30:12: error: instance variable '_age' accessed in class method return _age; ^ 3 errors generated.
|