黑马程序员---Objective-C @property和@synthesize
来源:互联网 发布:win10优化游戏性能 编辑:程序博客网 时间:2024/06/08 03:14
@property和@synthesize
#import@interface Person : NSObject{ int _age; int _height; double _weight; NSString *_name;}// @property:可以自动生成某个成员变量的setter和getter声明@property int age; //这一句等于下面的两句//- (void)setAge:(int)age;//- (int)age;@property int height;//- (void)setHeight:(int)height;//- (int)height;- (void)test;@property double weight;@property NSString *name; // 注意,此处name之前要有 *@end person.m#import "Person.h"@implementation Person// @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量@synthesize age = _age; 就相当于下面的语句 -(void)setAge:(int)age;{ _age = age;}-(int)age{ return _age;} @synthesize height = _height;@synthesize weight = _weight, name = _name;- (void)test{}@end
Car.h#import@interface Car : NSObject{ //int _speed; //int _wheels;}@property int speed;@property int wheels;// @property int speed, wheels;// 一般不这样写- (void)test;@end
Car.m#import "Car.h"@implementation Car//@synthesize speed = _speed, wheels = _wheels;// synthesize会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量@synthesize speed = _speed;@synthesize wheels = _wheels;- (void)test{ NSLog(@"速度=%d", _speed);}@end
Dog.h#import@interface Dog : NSObject@property int age;//这一句有三个功能//1、setter 声明和实现//2、getter 声明和实现//3、还能生成_age成员变量,并访问_age// property 自动生成的_age 成员变量是私有的,是在实现里需要的时候生成的@end
Dog.m#import "Dog.h"@implementation Dog// 如果实现里面没有setter 或 getter,就会默认@property自动生成的// 如果有下面手下的setter 或 getter,就会执行下面语句// 如果setter 和 getter 都有,不需要自动生成,那就不会自动生成 _ age成员变量了。// 只要setter 和 getter 有一个没写,就会自动生成- (void)setAge:(int)age{}@end
Cat.h#import@interface Cat : NSObject@property int age;@end
Cat.m#import "Cat.h"@implementation Cat// 因为后面没有 = _age 默认会访问age这个成员变量,如果没有age成员变量,就会自动生成@private类型的age变量@synthesize age;@end
main.m#import#import "Person.h"#import "Cat.h"#import "Dog.h"int main(int argc, const char * argv[]){ @autoreleasepool { Dog *d = [Dog new]; d.age = 5; NSLog(@"%d", d.age); } return 0;
0 0
- 黑马程序员---Objective-C @property和@synthesize
- 黑马程序员——Objective-C学习之@property @synthesize和id及点语法
- 黑马程序员----Objective-C学习笔记之@property关键字和@synthesize用法
- Objective-c @property和@Synthesize
- 黑马程序员-----@property和@synthesize
- 黑马程序员---@property和@synthesize
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- objective-c之@synthesize和@property含义
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Objective-C中的@property和@synthesize用法
- Java线程总结
- 今年暑假不AC
- JPA JPQL 查询、排序.....
- log4cplus学习指南(转)
- Android---40---实现XMl文件的序列化与解析
- 黑马程序员---Objective-C @property和@synthesize
- 技术路线的核心竞争力
- 基于Apache Mahout和Elasticsearch推荐系统介绍
- acm算法之三大背包问题
- Qt组件之QTablewidget积累
- CodeForces 151E Smart Cheater(线段树)
- Android ADB server didn't ACK * failed to start daemon * 和The connection to adb is down 简单有效的解决方案
- 排序算法(转)
- mac下passenger-install-nginx-module