先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个keyword的使用方法和含义,代码例如以下:

Person.h文件

#import <Foundation/Foundation.h>

@interface Person : NSObject {
int _age; //能够被子类訪问
//这里系统会帮我们生成一个默认的 int _no 私有变量(不能被子类訪问)
} @property int age;
@property int no; //自己写一个构造方法
- (id)initWithAge:(int)age andNo:(int)no; @end

Person.m文件

#import "Person.h"

@implementation Person

//Xcode 4.5以上都不用写以下两句(可省略,而且默认是_age和_no)
//@synthesize age = _age; //声明为protected
//@synthesize no = _no; //默认生成的是私有的 - (id)initWithAge:(int)age andNo:(int)no {
if(self = [super init]){
_age = age;
_no = no;
}
return self;
} - (NSString *)description {
return [NSString stringWithFormat:@"age is %i and no is %i", _age, _no];
} @end

main.m文件

#import <Foundation/Foundation.h>
#import "Person.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *person = [[Person alloc] initWithAge:15 andNo:2]; NSLog(@"age is %i and no is %i", person.age, person.no); [person setNo:3];
NSLog(@"no is %i", [person no]);
//%@代表打印一个OC对象
NSLog(@"%@", person);
}
return 0;
}

输出结果:

2014-11-12 21:53:15.406 firstOCProj[826:47802] age is 15 and no is 2

2014-11-12 21:53:15.407 firstOCProj[826:47802] no is 3

2014-11-12 21:53:15.408 firstOCProj[826:47802] age is 15 and no is 3

能够看到上面的代码简洁了不少,@property的作用就等价于对成员变量的声明,@synthesize的作用就等价于对成员变量setter和getter的标准实现。

须要注意的是:

1、在Xcode4.5以上能够不用写@synthesize属性(编译器默认加入)。

2、这样的写法能够和前面的写法(旧的写法)交叉使用。

3、假设要对setter或者getter方法有特殊处理。能够使用旧的写法(编译器就不会默认帮我们实现)。

4、假设没有显式的声明变量,则默认生成一个私有成员变量(不能被子类使用,如上面的_no)。

以下我们将上面代码该的更简单一些:

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property int age;
@property int no; @end

Person.m

#import "Person.h"

@implementation Person

@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *person = [Person new];
[person setAge:20];
[person setNo:3];
NSLog(@"age is %i and no is %i", person.age, person.no);
}
return 0;
}

输出结果:

2014-11-12 22:14:44.002 firstOCProj[849:52867] age is 20 and no is 3

注意:我的编译器Xcode是4.5以上的。所以能省略Person.m中的@synthesize属性。

这里不得不正确OC和Xcode编译器感叹。如此方便和好用的工具我真心是第一次见,默默的赞一下。

Objective-C基础笔记(2)@property和@synthesize的更多相关文章

  1. [Objective-c 基础 - 2.6] @property和@synthesize

    Xcode编译器的特性,自动生成getter和setter   A.@property 自动生成某个成员变量的getter和setter的声明 变量的命名要求:以下划线开头 Student.h @in ...

  2. @property和@synthesize的特性

    基础回顾:get方法和set方法 定义类成员变量时,可以在@interface中定义,也可以在@implementation中定义: 在@interface中声明,成员变量的状态是受保护的,即“@pr ...

  3. C#基础笔记---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  4. C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用

    C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备)  https://blog.csdn.net/u013519551/article/details/51220841 1. . ...

  5. iOS 内存管理-copy、 retain、 assign 、readonly 、 readwrite、nonatomic、@property、@synthesize、@dynamic、IB_DESIGNABLE 、 IBInspectable、IBOutletCollection

    浅谈iOS内存管理机制 alloc,retain,copy,release,autorelease 1)使用@property配合@synthesize可以让编译器自动实现getter/setter方 ...

  6. @synthesize obj=_obj的意义详解 @property和@synthesize

    本文转载至 http://blog.csdn.net/ztp800201/article/details/9231969 http://hi.baidu.com/feng20068123/item/c ...

  7. JavaScript基础笔记集合(转)

    JavaScript基础笔记集合   JavaScript基础笔记集合   js简介 js是脚本语言.浏览器是逐行的读取代码,而传统编程会在执行前进行编译   js存放的位置 html脚本必须放在&l ...

  8. UWP入门(二) -- 基础笔记

    原文:UWP入门(二) -- 基础笔记 不错的UWP入门视频,1092417123,欢迎交流 UWP-04 - What i XMAL? XAML - XML Syntax(语法) ,create i ...

  9. CSS3基础——笔记+实战案例(CSS基本用法、CSS层叠性、CSS继承性)

    CSS3基础——笔记 CSS是Cascading Style Sheet的缩写,翻译为"层叠样式表" 或 "级联样式表".CSS定义如何显示HTML的标签央视, ...

随机推荐

  1. WPF学习:绑定

    原文 http://www.cnblogs.com/SouthAurora/archive/2010/06/30/1768464.html 一.绑定到元素对象 1.元素和元素(XAML.代码) 1.1 ...

  2. docker 数据管理<1>

    1. 挂载本地的目录到容器里: docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录, :前面的/data为本地目录,:后面的/dat ...

  3. C语言的本质(12)——指针与函数

    往往,我们一提到指针函数和函数指针的时候,就有很多人弄不懂.下面详细为大家介绍C语言中指针函数和函数指针. 1.指针函数 当一个函数声明其返回值为一个指针时,实际上就是返回一个地址给调用函数,以用于需 ...

  4. @Transactional 注解说明

    先让我们看代码吧! 以下代码为在"Spring3事务管理--基于tx/aop命名空间的配置"基础上修改.首先修改applicationContext.xml如下: <pre ...

  5. (转)IOS笔记 #pragma mark的用法

    简单的来说就是为了方便查找和导航代码用的.   下面举例如何快速的定位到我已经标识过的代码.     #pragma mark 播放节拍器 - (void) Run:(NSNumber *)tick{ ...

  6. find the safest road

    find the safest road Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  7. dev gridcontrol 单箱效果

    private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChan ...

  8. ADT Example

    Example Data Types: Integer, and Character Example (Integer Data Type) The integer data type can con ...

  9. Angular基础知识

    AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 ng-directives 扩展了 HTML. ...

  10. 如何用SQL操作数据------告别标题党

    额,首先跟大家道一个歉,由于本人上次利用标题来骗访问,对各位大哥大姐,叔叔阿姨,弟弟妹妹,and舅子老表的时间及流量造成了严重的浪费,本人深表歉意(好吧,其实本人内心还是有那么一丢丢的自豪的,毕竟是一 ...