main.m
点语法
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
Person *p = [Person new];
[p setAge:50]; //set
p.age = 20; //点语法,也是一种set、get方法
// [p age]; //get方法
NSLog(@"age = %d",[p age]);
return 0;
}
Person.h
@interface Person : NSobject
{
@protected //受保护的,不是@public公开的,也不是@private私有的。
int _age;
}
- (void)setAge:(int)age;
- (void)age;
@end
Person.m
#import "Person.h"
@implementation Person
{
@private
int _weight;
}
- (void)setAge:(int)age
{
_age = age
{
- (void)age
{
return _age;
}
@end