您的位置:首页 > 职场人生

黑马程序员——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方法实现:

//注:为使使代码在一块清晰可见,我将.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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息