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

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

ios的@property屬性和@synthesize屬性

2019-11-09 16:28:52
字體:
供稿:網(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í),會自動(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 會自動(dòng)生成getter和setter的實(shí)現(xiàn)//@synthesize 默認(rèn)會去訪問age,no,height同名的變量,,//如果找不到同名的變量,會在內(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)說明:1.在Xcode4.5及以后的版本中,可以省略@synthesize ,編譯器會自動(dòng)幫你加上getter 和 setter 方法的實(shí)現(xiàn),并且默認(rèn)會去訪問_age這個(gè)成員變量,如果找不到_age這個(gè)成員變量,會自動(dòng)生成一個(gè)叫做 _age的私有成員變量。2.視頻教學(xué)中建議變量名用"_"前綴作為開頭,但我看big Nerd 那本書里是不用的,個(gè)人也比較習(xí)慣 big Nerd 的那種寫法,所以變量名就不加前綴了。 語法簡介:格式: 聲明property的語法為:@property (參數(shù)1,參數(shù)2) 類型 名字; 如:C代碼@property(nonatomic,retain) UIWindow *window;其中參數(shù)主要分為三類: 讀寫屬性: (readwrite/readonly)setter語意:(assign/retain/copy)原子性: (atomicity/nonatomic) 各參數(shù)意義如下: readwrite: 產(chǎn)生setter/getter方法readonly: 只產(chǎn)生簡單的getter,沒有setter。assign: 默認(rèn)類型,setter方法直接賦值,而不進(jìn)行retain操作retain: setter方法對參數(shù)進(jìn)行release舊值,再retain新值。copy: setter方法進(jìn)行Copy操作,與retain一樣nonatomic: 禁止多線程,變量保護(hù),提高性能
上一篇:仿IOS等待菊花效果

下一篇:iOS CAShapeLayer

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平定县| 昆山市| 固阳县| 丰顺县| 宁陕县| 宣化县| 营山县| 衡水市| 平塘县| 澎湖县| 通榆县| 德清县| 绍兴市| 莫力| 桂阳县| 宜都市| 筠连县| 伊川县| 东乡县| 天津市| 勐海县| 固始县| 武安市| 民权县| 阜南县| 绥德县| 承德市| 泰来县| 闵行区| 江源县| 黑河市| 泽普县| 雷州市| 慈利县| 石首市| 刚察县| 准格尔旗| 营山县| 吉隆县| 青海省| 曲靖市|