学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成;当然xcode这款编译器也很强大,也能自动生成;

1:@property

@property是写在类的声明中的,具体写法;

@interface  Person : NSObject
{
_age;
}
@property int age; // 这句话相当于下边的getter和setter,但是注意age没有'_',但是就是为有下划线的age生成的,这样方便了点语法的调用;
// - (void)setAge:(int)age;
// - (int)age;
@end

所以写法:@property   (成员变量的类型) 去掉下划线的成员变量名

2:@synthesize:setter和getter方法的实现,在implementation中;

@implementation

@synthesize  age = _age; // 注意格式,等号左边表示实现age的setter和getter,即 setAge  和  age; 等号右边表示访问那个成员变量;

// 代替了下面这两个方法;
/*
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
*/
@end

3:@property  @synthesize后边可以同时并列多个成员变量,前提是这几个成员变量的类型一样

// @property写法
@property int age, weight; // 以逗号分隔,前提是age和weight都是int型的;
// @synthesize写法
@synthesize age = _age, weight = _weight;

4:如果自己没有明确的定义成员变量,比如_age,然后写了@property int age;这句话会自动为我们生成_age这个成员变了,但是它的访问权限是private的

5:现在版本的@property功能更强大,独揽了setter和getter的声明和实现;可以只写@property,而不用写@synthesize,xcode也会自动生成setter和getter的声明与实现,并且你也可以不用定义成员变量,它也能自动为我们生成带有下划线的成员变量,只不过是private的;

@interface Student : NSObject
@property int age; // 这句话做了3件事
/*
1:为我们生成了_age这个成员变量,private的;
2:声明了age得setter和getter方法;
3:在implementation中实现了setter和getter
*/
@end

@property  @synthesize的使用注意;

如果@synthesize age;  这样写 他会访问的时和age同名的成员变量,不会访问_age;  如果没有age,他会自动生成@private的age;

方法也是一样,如果setter和getter我们自己写了,它会优先用我们自己写得,如果没有,它才会自动生成;xcode的特性就是这样,优先选择我们自己写得,如果我    们没写,它才自动生成;

id

万能指针,能指向任何OC对象,定义方式:id d = [Person new];注意定义时不要加*;如果说OC中所有的类都继承了NSObject,那么id相当于: NSObject *;

OC的@property 和 @synthesize id的更多相关文章

  1. OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  2. 李洪强iOS开发之OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  3. 「OC」@property @synthesize和id

    一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...

  4. Objective-C 点语法 成员变量的作用域 @property和@synthesize关键字 id类型

    点语法 1.利用点语法替换set方法和get方法 方法调用 Student *stu = [Student new]; [stu setAge : 18]; int age = [stu age]; ...

  5. OC中两个关键字的作用:@property和@synthesize

    两个关键字的使用:@property和@synthesize 一.@property关键字这个关键字是OC中能够快速的定义一个属性的方式,而且他可以设置一些值,就可以达到一定的效果,比如引用计数的问题 ...

  6. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...

  7. OC学习篇之---@property和@synthesize的使用

    在之前一片文章我们介绍了OC中的内存管理:http://blog.csdn.net/jiangwei0910410003/article/details/41924683,今天我们来介绍两个关键字的使 ...

  8. OC开发系列-@property和@synthesize

    property和synthesize 创建一个Person类.提供成员属性的_age和_height的setter和getter方法. #import <Foundation/Foundati ...

  9. OC基础--Property

    编译器指令: 用来告诉编译器要做什么 @property: @property是编译器的指令 告诉编译器在@interface中自动生成setter和getter的声明 @synthesize: @s ...

随机推荐

  1. Common工具类的验证码类的使用(未实现验证)

    验证码接收 using System; using System.Collections.Generic; using System.Linq; using System.Web; using CZB ...

  2. oledb,odbc简易实体生成器.

    DataSet to code file(C#)工具. 方便自己写的odbc或者oledb的东西用的. sql server或者oracle等大数据库已经有EF的支持. 一些其他数据库还是用oledb ...

  3. ORACLE之表

    本文章中的表在以后的例子中会用到. 首先有t_fn_person和t_fn_dept表. ) primary key not null, person_code ) not null, person_ ...

  4. Developers, do consider different user roles! - A bad experience with cron

    The Story: Last week, I found one of our embedded arm linux device  ran out of flash space( totally ...

  5. 熔断器设计模式<转>

    熔断器设计模式 如果大家有印象的话,尤其是夏天,如果家里用电负载过大,比如开了很多家用电器,就会”自动跳闸”,此时电路就会断开.在以前更古老的一种方式是”保险丝”,当负载过大,或者电路发生故障或异常时 ...

  6. 数据校验validator 与 DWZ

    在做系统时经常会用到数据校验,数据校验可以自己写,也可以用现在成的,现在记录下两种类库使用方法, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...

  7. VLAN系列

    Write From Yangwj Sunday, March 9, 2014 一. Vlan的识别 1. 交换机端口是访问端口,它就属于某一个Vlan:如果是中继端口,它就可以属于所有Vlan. 2 ...

  8. 第六篇、抽屉效果+UITabBarController(主流框架)

    依赖于第三方的框架RESideMenu 1.AppDelegate.m中的实现 - (BOOL)application:(UIApplication *)application didFinishLa ...

  9. path 环境变量

    path(环境变量)是dos以前的内部命令,windows继续沿用至今.用作运行某个命令的时候,本地查找不到某个命令或文件,会到这个声明的目录中去查找.一般设定java的时候为了在任何目录下都可以运行 ...

  10. 采用Service实现本地推送通知

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...