objective-c保护属性

#import <Foundation/Foundation.h>

@interface ClassVirable : NSObject{
    NSInteger year;//保护树形
}
@property int age;//不用在实现文件中写@synthesize age就会有setAge:和age方法了
-(void)set:(NSInteger)i;
@end
#import "ClassVirable.h"

@implementation ClassVirable
///Property implementation must have its declaration in interface 'ClassVirable'
//@synthesize year;
-(void)set:(NSInteger)i{
    self.age = i;
    year = i;
}
@end
#import <Foundation/Foundation.h>
#import "ClassVirable.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        ClassVirable *c = [[ClassVirable alloc]init];
        [c set:2];
        [c setAge:20];
        NSLog(@"%i",[c age]);
        NSLog(@"%i",c.age);
        //Instance variable 'year' is protected
//        NSLog(@"%ld",(long)c->year);
    }
    return 0;
}

 

posted @ 2014-10-15 09:34  wuhn  阅读(222)  评论(0编辑  收藏  举报