OC4_实例变量的作用域

//

//  Dog.h

//  OC4_实例变量的作用域

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface Dog : NSObject

{

    @protected//受保护的实例变量, 在当前类里面可以直接访问,在类外面不可以直接访问,但是可以通过调用方法间接访问,子类可以直接继承受保护的实例变量

    NSString *_name;

    NSInteger _age;

    @private  //私有的实例变量, 在当前类里面可以直接访问,在类外面不可以直接访问,但是可以通过调用方法间接访问,子类不可以继承私有的实例变量

    float _weight;

    float _height;

    @public   //公有的实例变量, 在类内类外都可以直接访问,子类可以直接继承公有的实例变量

    NSInteger _number;

}



//@property 关键字展开的实例变量是私有的



@property (copy,nonatomic) NSString *name;

@property (assign,nonatomic)NSInteger age;

@property (assign,nonatomic)float weight;

@property (assign,nonatomic)float height;

//@property (assign,nonatomic)NSInteger number;

- (void)setNumber:(NSInteger)number;

- (NSInteger)number;



- (void)printNumber;



@end
//

//  Dog.m

//  OC4_实例变量的作用域

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "Dog.h"



@implementation Dog

{

    NSInteger abc;

}



- (void)setNumber:(NSInteger)number

{

    _number = number;

}



- (NSInteger)number

{

    return self->_number;

}



- (void)printNumber

{

    NSLog(@"number = %li", [self number]);

}





@end
//

//  main.m

//  OC4_实例变量的作用域

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Dog.h"



int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Dog *xiaoHei = [[Dog alloc] init];

//        xiaoHei->_name = @"小黑";

//        xiaoHei->_age =12;

//        _name = @"xiaohei";

        xiaoHei.name =@"小黑";

        xiaoHei.age = 12;

        NSLog(@"name =%@ age = %li", xiaoHei.name, xiaoHei.age);

        

        xiaoHei.weight = 34.2;

        xiaoHei.height = 50;

        NSLog(@"weight  = %.2f height = %.2f", [xiaoHei weight], xiaoHei.height);

        

        xiaoHei->_number = 100;

        NSLog(@"number = %li", xiaoHei->_number);

        

    

    }

    return 0;

}

 

你可能感兴趣的:(实例变量)