国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

ios的@property屬性和@synthesize屬性

2019-11-09 15:13:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
當(dāng)你定義了一系列的變量時(shí),需要寫很多的getter和setter方法,而且它們的形式都是差不多的,,所以Xcode提供了@PRoperty和@synthesize屬性,@property用在 .h 頭文件中用作聲明,@synthesize用在.m 文件中用于實(shí)現(xiàn)。       如下,新建一個(gè)基于“Command Line Tool”的項(xiàng)目,名為“property”,再新建一個(gè)Student類,傳統(tǒng)的寫法是:Student.h復(fù)制代碼//  Student.h//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject{    int age;    int no;}//age的getter和setter方法聲明- (int)age;- (void)setAge:(int)newAge;//no的getter和setter方法聲明- (int)no;- (void)setNo:(int)newNo;@end復(fù)制代碼Student.m復(fù)制代碼////  Student.m//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import "Student.h"@implementation Student//age的getter和setter方法的實(shí)現(xiàn)- (int)age{    return age;}-(void)setAge:(int)newAge{    age = newAge;}//no的getter和setter方法的實(shí)現(xiàn)- (int)no{    return no;}- (void)setNo:(int)newNo{    no = newNo;}@end復(fù)制代碼main.m復(fù)制代碼////  main.m//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h" int main(int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        Student *stu = [[Student alloc] init];        stu.age = 100;//這句相當(dāng)于setter方法        NSLog(@"age is %i", stu.age);//這里的 stu.age 相當(dāng)于getter方法                [stu release];            }    return 0;}復(fù)制代碼 ************************************************************************************ 用@property和@synthesize的寫法是:Student.h復(fù)制代碼////  Student.h//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject{    int age;    int no;}//當(dāng)編譯器遇到@property時(shí),會(huì)自動(dòng)展開成getter和setter的聲明@property int age;@property int no;@end復(fù)制代碼Student.m復(fù)制代碼////  Student.m//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import "Student.h"@implementation Student//@synthesize 會(huì)自動(dòng)生成getter和setter的實(shí)現(xiàn)//@synthesize 默認(rèn)會(huì)去訪問(wèn)age,no,height同名的變量,,//如果找不到同名的變量,會(huì)在內(nèi)部自動(dòng)生成一個(gè)私有同名變量age,no,height,,//因此Student.h 中的這幾個(gè)變量也可以省略不寫。@synthesize age,no;@end復(fù)制代碼main.m復(fù)制代碼////  main.m//  property////  Created by Rio.King on 13-8-25.//  Copyright (c) 2013年 Rio.King. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){        @autoreleasepool {                // insert code here...        Student *stu = [[Student alloc] init];        stu.age = 100;        NSLog(@"age is %i", stu.age);                [stu release];    }    return 0;}復(fù)制代碼幾點(diǎn)說(shuō)明:1.在Xcode4.5及以后的版本中,可以省略@synthesize ,編譯器會(huì)自動(dòng)幫你加上getter 和 setter 方法的實(shí)現(xiàn),并且默認(rèn)會(huì)去訪問(wèn)_age這個(gè)成員變量,如果找不到_age這個(gè)成員變量,會(huì)自動(dòng)生成一個(gè)叫做 _age的私有成員變量。2.視頻教學(xué)中建議變量名用"_"前綴作為開頭,但我看big Nerd 那本書里是不用的,個(gè)人也比較習(xí)慣 big Nerd 的那種寫法,所以變量名就不加前綴了。 語(yǔ)法簡(jiǎn)介:格式: 聲明property的語(yǔ)法為:@property (參數(shù)1,參數(shù)2) 類型 名字; 如:C代碼@property(nonatomic,retain) UIWindow *window;其中參數(shù)主要分為三類: 讀寫屬性: (readwrite/readonly)setter語(yǔ)意:(assign/retain/copy)原子性: (atomicity/nonatomic) 各參數(shù)意義如下: readwrite: 產(chǎn)生setter/getter方法readonly: 只產(chǎn)生簡(jiǎn)單的getter,沒(méi)有setter。assign: 默認(rèn)類型,setter方法直接賦值,而不進(jìn)行retain操作retain: setter方法對(duì)參數(shù)進(jìn)行release舊值,再retain新值。copy: setter方法進(jìn)行Copy操作,與retain一樣nonatomic: 禁止多線程,變量保護(hù),提高性能
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江北区| 西安市| 聂荣县| 米脂县| 长春市| 克什克腾旗| 神木县| 安岳县| 林甸县| 沾益县| 莱西市| 东城区| 濮阳市| 柞水县| 藁城市| 嘉义市| 陕西省| 山丹县| 香格里拉县| 康马县| 阳新县| 循化| 嵊泗县| 垣曲县| 信宜市| 杭锦旗| 开江县| 苗栗县| 玛纳斯县| 惠安县| 宜阳县| 新泰市| 永福县| 四会市| 林西县| 潞西市| 临汾市| 黄石市| 明溪县| 明溪县| 保定市|