[关闭]
@fiy-fish 2015-07-14T03:55:38.000000Z 字数 964 阅读 1272

day01-03-实例变量的类型

Objective-c


  1. // day01-03-实例变量的类型
  2. //
  3. // Created by Aaron on 15/7/1.
  4. // Copyright (c) 2015年 Aaron. All rights reserved.
  5. //
  6. #import <Foundation/Foundation.h>
  7. #import "Person.h"
  8. int main(int argc, const char * argv[]) {
  9. @autoreleasepool {
  10. Person *p = [Person alloc];
  11. //在OC里面调用方法的表达式就是其返回值
  12. NSInteger money = [p showMoney];
  13. NSLog(@"知道输了%ld钱",money);
  14. //[p killPerson];
  15. }
  16. return 0;
  17. }

  1. #import <Foundation/Foundation.h>
  2. @interface Person : NSObject
  3. {
  4. @public
  5. //公开类型:在任意的地方都可以直接访问
  6. //通常情况是不使用这种类型
  7. NSInteger _age;
  8. @protected
  9. //保护类型:(默认缺省)在当前类以及其子类中都可以直接访问
  10. NSInteger _weight;
  11. @private
  12. //私有类型:只能在当前类里面可以直接访问
  13. NSInteger _money;
  14. }
  15. -(NSInteger)showMoney;
  16. //OC里的方法没有私有类型
  17. //但是可以通过不提供接口的形式来实现方法的私有化
  18. //-(void)killPerson;
  19. @end

  1. #import "Person.h"
  2. @implementation Person
  3. -(NSInteger)showMoney
  4. {
  5. _money = 50000;
  6. NSLog(@"%ld",_money);
  7. return _money;
  8. }
  9. -(void)killPerson
  10. {
  11. NSLog(@"我杀了人");
  12. }
  13. @end

  1. #import "Person.h"
  2. @interface Girl : Person
  3. -(void)showWeight;
  4. @end

  1. #import "Girl.h"
  2. @implementation Girl
  3. -(void)showWeight
  4. {
  5. NSLog(@"%ld",_weight);
  6. //NSLog(@"%ld",_money);子类不能直接访问父类的私有属性
  7. }
  8. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注