OBJ-C @property和@synthesize关键字学习笔记

来源:互联网 发布:java重写的作用 编辑:程序博客网 时间:2024/06/05 05:33
============================================================
                 @property和@synthesize关键字
===========================================================


@property


格式: @property int age;相当于声明了实例变量的set和get方法;


放在@interface @end 之间;


@synthesize


格式: @synthesize age;相当于实现了实例变量的set和get方法;


放在@implementation @end之间;


---------------------------------------------


@synthesize age 写完后,系统会自动帮我们生成一个实例变量age


@synthesize age的展开:(重点!!!!!)


                -(void)setAge:(int)age{
                     self->age = age;
                     }


                -(int)age{
                     return age;
                     }
------------------------------------------------------


【掌握】@synthesize指定实例变量赋值


例如:  @synthesize age = _b;


就相当于: -(void)setAge:(int)age
              {
                _b = age;
              }
           -(int)age
              {
                 return _b;
              }
意思就是它不会创建一个age的实例变量了,而是一个_b的实例变量;


-------------------------------------------------------------


@property int age,weight,height;
@property NSString *name;


(@property可以这样连续声明,但是不同类型的要区分开)


@synthesize age,weight,height,name;


(@synthesize可以这样连续实现,即使类型不同也可以放到一块,但最好  做下区分)


==========================================================


@property增强用法


1.xcode4.4之后,可以只使用@property 而不适用@synthesize
2.声明和实现了_age等实例变量的set和get方法
3.操作的是带有下划线的实例变量;
4.如果我们当前类,没有下划线的实例变量,则系统会帮我们自动生成;
5.生成的下划线变量是私有的,不允许被子类继承


===========================================================


@property增强下重写set和get方法


在.m中只能重写一个,不能同时手动实现!!!重写一般是为了加上限定条件。


如果都要重写的话,一定要加上@synthesize 的实现。
0 0