学习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. 四舍五入PK银行四舍五入

    描述 在实际开发中decimal.Round(1.23525,4)!=1.2353实际是1.2352,而decimal.Round(1.23535,4)==1.2354 说明 四舍五入:当舍去位的数值 ...

  2. 十大Intellij IDEA快捷键(转)(2015年06月15日)

    注:本文转自:http://blog.csdn.net/dc_726/article/details/42784275 Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow ...

  3. 判断checked是否选中

    if($('#checkbox-id').is(':checked')) {     // do something }

  4. django 学习-18 用户管理Auth系统使用

    1.首先跟之前说的admin的要求有点像, vim urls.py from django.contrib import adminadmin.autodiscover()               ...

  5. EventDemoandStyleDemoandThemeDemo

    Event Handling 示例: 分为EventListener. EventListenerRegistration和EventHandler. 注册Event的三种方法: 1)  在Activ ...

  6. ASP.NET MVC3 301永久重定向实现程序

    使用 ASP.NET 又喜欢跟进新技术的朋友可能已经知道,在 ASP.NET 4.0 中增加了 Response.RedirectPermanent() 方法来实现永久重定向,方法的作用在注释中解释的 ...

  7. Android App测试要点

         本文主要内容,转载自 http://www.51testing.com/html/04/344504-849373.html,      在这里,主要是整理一下app测试的总体思路,这里的a ...

  8. How to Fix Missing TortoiseSVN File Status Icons in Windows

    For many Windows-based developers, Subversion and TortoiseSVN is a great source control solution. It ...

  9. C++实现设计模式之 — 简单工厂模式

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4251756.html 所谓简单工厂模式,是一种实例化对象的方式,只要输入需要实例化对象的名字 ...

  10. zedboard OPENCV移植

    1:系统环境搭建 要准备好交叉编译环境 见http://blog.csdn.net/xiabodan/article/details/22717175 2:下载cmake CMake是一个跨平台的安装 ...