黑马程序员——OC基础学习(三)---从传统setter方法和getter方法到@property增强型使用(体验代码的优化过程)
2015-06-29 20:44
921 查看
------ <a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>
期待与您交流! -------
从传统setter方法和getter方法到@property
增强型使用---体验代码的优化过程
注:在以下文章中setter方法简写为:set方法,getter方法简写为:get方法.
题目要求:
定义一个Person的类,并包含姓名,年龄,身高,体重,性别,设定并打印出年龄的值.
一.用传统set方法和get方法实现:
二.通过@property和get方法来实现
1.@property是一个编译器的特性(编译器编译到这个地方,要进行替换),替换成对应的实例变量的get方法和set方法的声明.
2.@property用法
@property 形参类型 方法名
3.例题的实现
三.通过@property和@synthesize来实现
1.@synthesize是一个编译器的特性(编译器编译到这个地方,要进行替换),替换成对应的实例变量的get方法和set方法的实现.
2.@property用法
@synthesize XXX (注:这里的XXX为@property中的方法名)
3.例题的实现
四.通过@property和@synthesize来实现的代码进一步的优化
五.通过@property增强型来实现
1.@synthesize增强型只在.h中使用@property,不在.m中使用@synzhesize,并同时声明和实现get方法和set方法.
2.@property增强用法
@property 形参类型 方法名
3.例题的实现
期待与您交流! -------
从传统setter方法和getter方法到@property
增强型使用---体验代码的优化过程
注:在以下文章中setter方法简写为:set方法,getter方法简写为:get方法.
题目要求:
定义一个Person的类,并包含姓名,年龄,身高,体重,性别,设定并打印出年龄的值.
一.用传统set方法和get方法实现:
//注:为使使代码在一块清晰可见,我将.h和.m中的代码都放在main.m文件中 #import <Foundation/Foundation.h> //声明实例变量 @interface Person : NSObject { NSString *_name; int _age; float _height; float _weight; int _sex; } //set方法和get方法的声明 -(void)setName:(NSString *) name; -(NSString *)name; -(void)setAge:(int)age; -(int)age; -(void)setHeight:(float)height; -(float)height; -(void)setWeight:(float)weight; -(float)weight; -(void)setSex:(int)sex; -(int)sex; @end #import "Person.h" //set方法和get方法的实现 @implementation Person -(void)setName:(NSString *) name{ _name = name; } -(NSString *)name{ return _name; } -(void)setAge:(int)age{ _age = age; } -(int)age{ return _age; } -(void)setHeight:(float)height{ _height = height; } -(float)height{ return _height; } -(void)setWeight:(float)weight{ _weight= weight; } -(float)weight{ return _weight; } -(void)setSex:(int)sex{ _sex = sex; } -(int)sex{ return _sex; } @end #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; //为实例变量赋值并打印 [p setAge:18]; NSLog(@"age = %d",[p age]); } return 0; }
二.通过@property和get方法来实现
1.@property是一个编译器的特性(编译器编译到这个地方,要进行替换),替换成对应的实例变量的get方法和set方法的声明.
2.@property用法
@property 形参类型 方法名
3.例题的实现
#import <Foundation/Foundation.h> //声明实例变量 @interface Person : NSObject { NSString *_name; int _age; float _height; float _weight; int _sex; } //set方法和get方法的声明 @property NSString* name; @property int age; @property float height; @property float weight; @property int sex; @end #import "Person.h" //set方法和get方法的实现 @implementation Person -(void)setName:(NSString *) name{ _name = name; } -(NSString *)name{ return _name; } -(void)setAge:(int)age{ _age = age; } -(int)age{ return _age; } -(void)setHeight:(float)height{ _height = height; } -(float)height{ return _height; } -(void)setWeight:(float)weight{ _weight= weight; } -(float)weight{ return _weight; } -(void)setSex:(int)sex{ _sex = sex; } -(int)sex{ return _sex; } @end #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; //为实例变量赋值并打印 [p setAge:18]; NSLog(@"age = %d",[p age]); } return 0; }
三.通过@property和@synthesize来实现
1.@synthesize是一个编译器的特性(编译器编译到这个地方,要进行替换),替换成对应的实例变量的get方法和set方法的实现.
2.@property用法
@synthesize XXX (注:这里的XXX为@property中的方法名)
3.例题的实现
//注:为使使代码在一块清晰可见,我将.h和.m中的代码都放在main.m文件中 #import <Foundation/Foundation.h> //声明实例变量 @interface Person : NSObject { NSString *_name; int _age; float _height; float _weight; int _sex; } //通过@property来实现set方法和get方法的声明 @property NSString* name; @property int age; @pr 4000 operty float height; @property float weight; @property int sex; @end #import "Person.h" //通过@synthesize来实现set方法和get方法的实现 @implementation Person @synthesize age; @synthesize name; @synthesize height; @synthesize weight; @synthesize sex; @end #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; //为实例变量赋值并打印 [p setAge:18]; NSLog(@"age = %d",[p age]); } return 0; }
四.通过@property和@synthesize来实现的代码进一步的优化
//声明实例变量 @interface Person : NSObject { NSString *_name; int _age; float _height; float _weight; int _sex; } //通过@property来实现set方法和get方法的声明 @property NSString* name; @property int age,sex; @property float height,weight; @end #import "Person.h" //通过@synthesize来实现set方法和get方法的实现 @implementation Person @synthesize age,name,height,weight,sex; @end #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; //通过点语法为实例变量赋值并打印 p.age = 18; NSLog(@"age = %d",p.age); } return 0; }
五.通过@property增强型来实现
1.@synthesize增强型只在.h中使用@property,不在.m中使用@synzhesize,并同时声明和实现get方法和set方法.
2.@property增强用法
@property 形参类型 方法名
3.例题的实现
//注:为使使代码在一块清晰可见,我将.h和.m中的代码都放在main.m文件中 #import <Foundation/Foundation.h> @interface Person : NSObject //通过@property增强型来实现set方法和get方法的声明和实现 @property NSString* name; @property int age,sex; @property float height,weight; @end #import "Person.h" @implementation Person @end #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [Person new]; //通过点语法为实例变量赋值并打印 p.age = 18; NSLog(@"age = %d",p.age); } return 0; }
相关文章推荐
- #无私分享《传智播客2011版。net平台c#视频》全套哦30.7G
- 黑马程序员---Java面向对象编程思想小结
- 黑马程序员---java面向抽象/接口编程
- 黑马程序员---java单例设计模式
- 黑马程序员---java字符串常用类
- 黑马程序员宣传语征集大赛
- 黑马程序员-------关于今天的一个程序学习心得
- 黑马程序员----把Eclipse修改为黑色主题颜色方案
- c基础语法(一)---流程控制与函数
- C基础语法(二)---复杂数据类型(数组和字符串)
- C基础语法(三)---复杂数据类型(指针)
- OC基础语法(三)---核心语法(1)
- OC基础语法(四)---核心语法(2)
- OC基础语法(五)---内存管理
- OC基础语法(六)---内存管理(二)
- OC基础语法(七)---Foundation框架
- 黑马程序员
- 程序员常用英语大集合 http://bbs.itheima.com/thread-118950-1-1.html (出处: 黑马程序员IT技术论坛)
- IO(输入输出流) 4000
- 黑马程序员_多线程(1)