f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 {
 5     int _age;
 6 }
 7 
 8 - (void)setAge:(int)age;
 9 - (int)age;
10 @end
11 
12 @implementation Person
13 
14 - (void)setAge:(int)age
15 {
16     _age = age;
17     NSLog(@"调用了setAge方法");
18 }
19 - (int)age
20 {
21     NSLog(@"调用了age方法");
22     return _age;
23 }
24 @end
25 
26 int main() {
27 
28 
29     Person *p = [Person new];
30     p.age = 10;        // 当点语法作为左值调用setter方法
31     int a = p.age;    // 当点语法作为右值调用getter方法
32     NSLog(@"age = %i", a);
33     return 0;
34 }
35 
36 /*
37 输出
38 2015-05-01 14:40:44.890 a.out[1849:11976] 调用了setAge方法
39 2015-05-01 14:40:44.891 a.out[1849:11976] 调用了age方法
40 2015-05-01 14:40:44.891 a.out[1849:11976] age = 10
41 
42 
43 总结:
44 1.点语法的本质就是方法的调用
45 2.当作为左值调用setter方法,反之调用getter方法。
46 */2015/5/1下午2:42:39
posted on 2015-05-01 14:43 swp 阅读(77) 评论(0)  编辑 收藏 引用 所属分类: objective-c

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理