//
// main.m
// 属性生成器
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person * p = [[Person alloc] init];
[p setAge:10];
[p setName:@"小明"];
/*
NSLog(@"年龄 %d",p->_age);
NSLog(@"年龄get %d",p.age);
NSLog(@"number %d",p->_number);
*/
NSLog(@"%@",p);
return 0;
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
/*
成员变量的作用域
@public 最大,完全公开成员变量
@private 只有在自己内部才可以直接访问
@protected 只有在自己,或者子类内部才可以直接访问,默认都是@protected
*/
/*
@public
NSString * _name;
int _age;
int _number;
*/
}
//属性生成器
//1.能够自动的生成get,与set方法的声明
//2.告诉@property set方法的参数类型是什么,告诉他属性的名称是什么
//3.@property是编译器特性
@property int age;
@property NSString * name;
/*
- (void)setAge:(int)age;
- (int)age;
-(void)setAge:(int)age;
-(int)age;
- (void)setName:(NSString *)name;
- (NSString *)name;
*/
@end
#import "Person.h"
@implementation Person
{
int _age;
}//@implementation 当中可以声明成员变量,外部无法访问
@synthesize age = _age;
@synthesize name = _name;
*/
///*
//- (void)setAge:(int)age
//{
// _number = age;
//}
//
// - (void)age
// {
// return _number;
// }
//
//
//-(void)setAge:(int)age
//{
// _age = age;
//
//}
//-(int)age
//{
// return _age;
//}
//*/
//- (void)setName:(NSString *)name
//{
// _name = name;
//
//}
//- (NSString *)name
//{
//
// return _name;
//
//}
-(void)setAge:(int)age
{
if (age > 0)
{
_age = age;
}
}
- (int)age
{
return _age;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"age = %d name %@", _age,_name];
}
@end