黑马程序员——OC-Setter/Getter函数及@proterty和@synthesize
来源:互联网 发布:中国历史延续 知乎 编辑:程序博客网 时间:2024/04/30 08:57
——- android培训、java培训、期待与您交流! ———-
Setter函数
setter函数,对成员变量赋值。Set函数的一般写法以对age操作为例,写法为:
-(void)setName:(NNString*)name; //声明setter函数
Get函数
getter函数,对成员变量取值。Get函数的一般写法也以对age的操作为例,写法为:
-(int)age; //声明getter函数
具体实现如下例子:
Person.h
@interface Person : NSObject { int age; @protected float height; } - (int) age; //get方法 - (void) setAge:(int)pAge; //set方法 @end
Person.m
#import <Foundation/Foundation.h> #import "Person.h" @implementation Person - (int) age { return age; } - (void) setAge:(int)pAge { age = pAge; } @end
main.m
#import <Foundation/Foundation.h>#import "Person.h"int main() { Person* per = [[Person alloc] init]; int age = [per age]; //调用get方法 [per setAge:16]; //调用set方法 //使用"." 来调用get/set使用的都是原始变量名,这就要求变量的get、set都符合约定 int age2 = per.age; //get per.age = 17; //set return 0; }
@proterty和@synthesize
每次这样写get/set方法,很麻烦,OC有一个自动化的方法,即使用@proterty和@synthesize关键字可以自动完成getter/setter方法的声明及实现。
具体如下所示:
Person.h
@interface Person : NSObject { int age; @protected // float height; } //- (int) age; //- (void) setAge:(int)pAge; @property int age; //编译器自动解释成 int age的get/set方法 的声明。 //@property int age = _age;//如果没有指定成员变量名,实现中默认访问的同名的成员变量age @property float height; //如果height没有声明,而用在这里, 也会自动生成以height为标准名的 get/set方法 @end
Person.m
#import <Foundation/Foundation.h> #import "Person.h" @implementation Person @synthesize age; //编译器自动解释成 age的get、set方法实现。 在xcode4.5之后可以不写这句话 @synthesize height = _height; //如果_height不存在,会生成一个私有的_height变量 @end
@proterty的增强
Person.h
@interface Person : NSObject { int age; @protected // float height; } //- (int) age; //- (void) setAge:(int)pAge; @property int age; //编译器自动解释成 int age的get/set方法 的声明以及实现。 @property float height; //如果height没有声明,而用在这里, 也会自动生成以height为标准名的 get/set方法 并 将其实现@end
Person.m
#import <Foundation/Foundation.h> #import "Person.h" @implementation Person // @proterty增强的功能已经将getter/setter方法实现,不需要自己手动编写@end
0 0
- 黑马程序员——OC-Setter/Getter函数及@proterty和@synthesize
- 黑马程序员——OC 基础:setter和getter方法
- [黑马程序员][OC]setter和getter
- 黑马程序员——封装setter和getter
- 黑马程序员-OC-getter & Setter方法
- 10、OC setter和getter (@property、@synthesize)
- <黑马程序员>setter方法和getter方法
- 黑马程序员 —OC(@property和@synthesize)
- 黑马程序员——OC基础学习(三)---从传统setter方法和getter方法到@property增强型使用(体验代码的优化过程)
- 黑马程序员_Objective C中的@property 与 @synthesize 快速生成setter getter方法
- 黑马程序员_Objective C中的@property 与 @synthesize 快速生成setter getter方法
- setter,getter,@property,@synthesize
- 黑马程序员------OC基础-----@property和@synthesize
- 黑马程序员-------OC----@property和@synthesize
- 黑马程序员_关于setter和getter知识总结
- 黑马程序员——OC语言@property@synthesize
- 黑马程序员——OC基础---@property @synthesize
- 黑马程序员——OC基础——@property、@synthesize和id类型
- Android中SwipeRefreshLayout的使用
- 程明明-关于文献阅读和寻找新的科研题目
- hdu 1269 迷宫城堡
- 临界区,互斥量,信号量,事件的区别
- java基础之对properties配置文件的读写操作
- 黑马程序员——OC-Setter/Getter函数及@proterty和@synthesize
- 黑马程序员---集合框架(一)
- 数据结构例程——图的邻接矩阵存储结构及算法
- Java 8中的增强的Arrays
- 《剑指offer》——斐波那契数列
- 从系统角度看分布式系统的挑战
- 简单的二叉树创建及其基本的运算(C语言)
- OpenCV中IplImage图像格式与BYTE图像数据的转换
- Android录音播放工具类,仿微信语音聊天。