@property和@synthesize
来源:互联网 发布:夏普比率算法 编辑:程序博客网 时间:2024/06/15 13:31
现在好多刚入门的开发者对于这个@property和@synthesize这两个关键字弄不清楚,看公司里面的代码,好多老程序员留下的代码里面都有这个关键字,这个是衡量一个程序员是不是老程序员的关键,但不是衡量一个优秀的程序员的关键,为了咱们成为更优秀的程序员,为了更好的阅读代码,现在咱们就彻底弄清楚这写关键字是啥意思。
@interface Person : NSObject
{
@public//这个代表这个成员变量是公共的,可以在外界访问,比如在main.m中访问用对象p->_age访问
@protected//这个是默认的,受保护的,成员变量只能在自己内部或者在子类内部访问
@private//这个是私有的,成员变量只能在自己的内部访问
@package//只能在本框架内使用,不常用,不多做介绍
NSInteger _age;
}
@property NSInteger age;
-(void)test;
@end
比如一个Person.h里面@propertyNSInteger age;这一句就是代表在声明了setter和getter方法
在早期的Xcode里面不会自动帮你写setter和getter方法,所以在Person.m里面@synthesize age = _age;这一句就是代表了实现了_age这个成员变量的setter和getter方法,这样在person类里面会自动创建_age这个成员变量,但是这个成员变量是私有的,只能在自己类的内部访问,其子类在内部也不能访问,或则通过setter和getter方法访问,如果想让其在子类内部访问,就在person.h里面声明一下成员变量,就像这样
@interface Person : NSObject
{
NSInteger _age;
}
@property NSInteger age;
-(void)test;
@end
这样就能在子类内部访问这个成员变量了,
如果想让其在外部不通过setter和getter方法访问了,直接用对象访问,那就加上关键字
@interface Person : NSObject
{
@public
NSInteger _age;
}
@property NSInteger age;
-(void)test;
@end
@interface Person : NSObject
{
NSInteger _age;
}
这样就能在其子类里面访问_age了。
- @property和@synthesize浅解
- @property和@synthesize见解
- property和synthesize
- @property和@synthesize
- @synthesize和@property详解
- C15_OC05-@property和@synthesize
- @property和@synthesize
- property 和 synthesize解说
- @property和@synthesize
- iOS_@property和@synthesize
- @property和@synthesize
- 08-property和synthesize
- @property和@synthesize
- @property 和 @synthesize
- oc_study16 @property和@synthesize
- 关键字@property和@synthesize
- iOS@synthesize 和 @property
- @property和@synthesize
- NotificationManager 通知
- 利用github搭建个人maven仓库
- Sqlserver得到周的结束日期
- 401. Binary Watch的C++解法
- 利用pandas进行数据分析之二:DataFrame与Series数据结构对比
- @property和@synthesize
- ZMQ(java)-编译的心酸历程
- 事物注解方式: @Transactional
- AttributeError: 'module' object has no attribute 'text_format'
- 面经
- jQuery源码分析-----pushStack
- LeetCode-string-Longest Substring Without Repeating Characters
- linux centos FTP服务器搭建vsftpd配置
- Android实现透明状态栏方法以及改变状态栏文字颜色