objective-c set/get方法(自定义自己的set/get方法)
2016-02-24 23:26
501 查看
1 新建一个类 命名为people
#import <Foundation/Foundation.h>
@interface people :
NSObject{
int _age;
}
@property int age;//这句写后将自动生成set/get方法
setAge:(int)age
@end
//自定义自己的set/get方法
2
#import "people.h"
@implementation people
-(void)setAge:(int)age{
NSLog(@"set age : ");
_age=age;
}
//自定义set方法
-(int)age{
NSLog(@" get age: ");
return _age;
}
//自定义get方法
@end
3 //主调函数中使用这个方法,并根据输出可以看出编译器如何得到这个数,并且进行输出的
#import <Foundation/Foundation.h>
#import "people.h"
int main(int argc,const
char * argv[]) {
@autoreleasepool {
people *p=[[peoplealloc]init];
[p setAge:19];
NSLog(@" the age is %d ",p.age);
}
return 0;
}
程序输出结果为:
2016-02-24 23:17:36.170 get方法[1602:757938] set age :
2016-02-24 23:17:36.171 get方法[1602:757938] get age:
2016-02-24 23:17:36.171 get方法[1602:757938] the age is 19
Program ended with exit code: 0
#import <Foundation/Foundation.h>
@interface people :
NSObject{
int _age;
}
@property int age;//这句写后将自动生成set/get方法
setAge:(int)age
@end
//自定义自己的set/get方法
2
#import "people.h"
@implementation people
-(void)setAge:(int)age{
NSLog(@"set age : ");
_age=age;
}
//自定义set方法
-(int)age{
NSLog(@" get age: ");
return _age;
}
//自定义get方法
@end
3 //主调函数中使用这个方法,并根据输出可以看出编译器如何得到这个数,并且进行输出的
#import <Foundation/Foundation.h>
#import "people.h"
int main(int argc,const
char * argv[]) {
@autoreleasepool {
people *p=[[peoplealloc]init];
[p setAge:19];
NSLog(@" the age is %d ",p.age);
}
return 0;
}
程序输出结果为:
2016-02-24 23:17:36.170 get方法[1602:757938] set age :
2016-02-24 23:17:36.171 get方法[1602:757938] get age:
2016-02-24 23:17:36.171 get方法[1602:757938] the age is 19
Program ended with exit code: 0
相关文章推荐
- Objective-C中字符串(NSString和NSMutableString)常用的方法
- 从C/C++到Objective-C(二)--- 面向对象
- [iOS]Objective-C编程规范
- 【JSON异常系列】new JSONObject对象时卡死原因
- Anaconda下import caffe出现错误TypeError: 'module' object is not callable
- Objective-C 内存管理之 _ARC
- Animation动画详解(七)——ObjectAnimator基本使用
- iOS UIView动画详解(Objective-C)
- Retrofit2.0 使用归纳和 JSONObject Conveter
- Java IO之对象的序列化、ObjectInputStream和ObjectOutputStream类
- iOS NSUserDefaults setObject forKey,本地化存储,删除字典中的null,空值,本地化的时候,如果value为空值所引起的崩溃
- Swift 与 Objective-C混合编程
- TypeError: ObjectId('') is not JSON serializable
- JSONObject_v3
- Java基础(二):Object、Class、克隆、异常编程
- objective_C 优缺点
- 目标检测“Object Detection Using Generalization and Efficiency Balanced Co-occurrence Features”
- Spring中JDBCTemplate使用queryForObject方法时 ”Incorrect column count: expected 1, actual 5“错误
- Spring中JDBCTemplate中queryForObject()方法的使用
- Objective-C Runtime 运行时之六:拾遗