黑马程序员------OC基础-----@property和@synthesize
2015-02-08 20:43
197 查看
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
@property : 可以自动生成某个成员变量的setter和getter声明
@synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
请看示例:
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
********************************************************************
@property : 可以自动生成某个成员变量的setter和getter声明
@synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量
请看示例:
#import <Foundation/Foundation.h> #import "Person.h" #import "Car.h" #import "Dog.h" #import "Cat.h" int main() { @autoreleasepool { Dog *d = [Dog new]; d.age = 5; NSLog(@"%d", d.age); } return 0; }
********************************************************************
<pre name="code" class="objc">#import <Foundation/Foundation.h> @interface Person : NSObject { int _age; //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; @end
********************************************************************
#import "Person.h" @implementation Person // @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量 @synthesize age = _age; @synthesize height = _height; @synthesize weight = _weight, name = _name; @end
********************************************************************
#import <Foundation/Foundation.h> @interface Car : NSObject { //int _speed; //int _wheels; } @property int speed; @property int wheels; //@property int speed, wheels; - (void)test; @end
********************************************************************
#import "Car.h" @implementation Car //@synthesize speed = _speed, wheels = _wheels; // 会访问_speed这个成员变量,如果不存在,就会自动生成@private类型的_speed变量 @synthesize speed = _speed; @synthesize wheels = _wheels; - (void)test { NSLog(@"速度=%d", _speed); } @end
********************************************************************
#import <Foundation/Foundation.h> @interface Dog : NSObject @property int age; @end
********************************************************************
#import "Dog.h" @implementation Dog - (void)setAge:(int)age { } @end
********************************************************************
#import <Foundation/Foundation.h> @interface Cat : NSObject { //int _age; //int age; } @property int age; @end
********************************************************************
#import "Cat.h" @implementation Cat // 默认会访问age这个成员变量,如果没有age,就会自动生成@private类型的age变量 @synthesize age; @end
相关文章推荐
- 黑马程序员——OC基础---核心语法(点语法,成员变量的作用域,@property和@synthesize)
- 黑马程序员——OC基础语法—@property和@synthesize使用
- 黑马程序员--OC基础--@property和@synthesize关键字介绍
- 黑马程序员 --- OC中@property和@synthesize的用法
- 黑马程序员学习笔记_OC之@property和@synthesize
- 黑马程序员-------OC----@property和@synthesize
- 黑马程序员——OC语言学习——Xcode开发初始化、点语法、成员变量作用域、关键字@property和@synthesize
- 黑马程序员---Objective-C基础学习---编译器特性@property和@synthesize
- 黑马程序员——OC基础——@property、@synthesize和id类型
- 黑马程序员-我的第十篇学习日记:OC中点语法,@property和@synthesize,完整创建一个可用对象
- 黑马程序员——OC基础学习(三)---从传统setter方法和getter方法到@property增强型使用(体验代码的优化过程)
- 黑马程序员——OC基础--@property在开发中需要注意的使用细节
- 黑马程序员_iOS开发之OC之面向对象之多文件操作、点语法、成员变量的作用域和关键字@property与@synthesize
- 黑马程序员——OC语言基础---@synthesize关键字介绍和使用
- OC基础--关键字@property 和 @synthesize
- 黑马程序员---OC基础---封装、继承、多态、@property、id类型、实例变量修饰符
- OC基础---NSString、多文件开发、点语法、成员变量的作用域、@property和@synthesize
- 黑马程序员 —OC(@property和@synthesize)
- 黑马程序员———OC学习第三天(@property和@synthesize关键字)
- OC基础第四章:@property和@synthesize