[关闭]
@fiy-fish 2015-05-18T16:10:07.000000Z 字数 829 阅读 1157

实例变量的类型

Objective-c


  1. // main.m
  2. // 实例变量的类型
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Person.h"
  9. int main(int argc, const char * argv[])
  10. {
  11. @autoreleasepool {
  12. Person *p = [Person alloc];
  13. [p showWeight];
  14. NSLog(@"女孩子的体重是:%d",
  15. [p tellWeight]);
  16. }
  17. return 0;
  18. }
  1. // Person.h
  2. // 实例变量的类型
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. @interface Person : NSObject
  9. {
  10. //修饰实例变量
  11. @protected
  12. //默认缺省的类型
  13. //保护类型
  14. //可以在当前类以及其子类里面直接进行访问
  15. int _weight;
  16. @private
  17. //私有类型
  18. //只能在当前类里面直接进行访问
  19. @public
  20. //公有类型
  21. //可以在任何地方直接访问
  22. //不安全,违背了封装的思想,所以一般都不用
  23. }
  24. //在OC里面,方法没有私有类型
  25. //但是我们可以通过不提供接口来实现方法的私有化
  26. -(void)showWeight;
  27. -(int)tellWeight;
  28. @end
  1. #import "Person.h"
  2. @implementation Person
  3. -(void)showWeight
  4. {
  5. _weight = 100;
  6. NSLog(@"我的体重是%d",
  7. _weight);
  8. }
  9. -(int)tellWeight
  10. {
  11. return _weight-10;
  12. }
  13. -(void)saveMe
  14. {
  15. NSLog(@"saveMe");
  16. }
  17. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注