先贴出使用@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. SSM整合中遇到的不能扫描注解的bug

    我的开发环境为: ubuntu14.04LTS 64bit; Spring Tool Suite  3.5.0.RELEASE Maven 3.2.3 SSM整合中遇到的不能扫描注解的bug 最终解决 ...

  2. Android stagefright与opencore对比

      [转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...

  3. C++字符串之一(字符表示)

    在C++中有两种类型可以用于表示字符,char和wchar_t. 但是字符串格式的标准却有很多种,如ASCII,UTF8,UTF16,UTF32等等.字符串的格式和char/wchar_t 的关系是什 ...

  4. Codeforces 325D

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  5. SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出

    ),)) as begin set nocount on ) ) ) select @sqlstr='select ''insert '+@tablename select @sqlstr1='' s ...

  6. click through rate prediction

    包括内容如下图: 使用直接估计法,置信区间置信率的估计: 1.使用二项分布直接估计 $p(0.04<\hat{p}<0.06) = \sum_{0.04n\leq k \leq 0.06n ...

  7. 一张图告诉你如何优化web 性能

  8. istream类的公有成员函数

    1 eatwhite 2 get 3 getline 4 gcount 5 ignore 6 operator>> 7 peek 8 read 9 seekg 10 tellg 1 eat ...

  9. CSS实现倒影-------Day80

    发现这个功能的时候非常开心,结果不想居然是个残次品,让我不禁想起了"天龙八部"上段誉的六脉神剑,在这个浏览器上能够.在还有一个上就无论了啊,时灵时不灵的,只是有总比没有要来的好,一 ...

  10. 获取xml文件

    <?xml version="1.0" encoding="utf-8" ?><ArrayOfSystemRool xmlns:xsi=&qu ...