IOS之構(gòu)造方法與自定義構(gòu)造方法的區(qū)別與實現(xiàn)
更新時間:2015年07月21日 14:37:04 作者:-FIGHTING-
本篇文章主要介紹了構(gòu)造方法以及自定義構(gòu)造方法的實現(xiàn),需要的朋友可以參考下
構(gòu)造方法,也就是int方法,不接受任何的參數(shù),而在實際的開發(fā)過程中,為了方便,會經(jīng)常自定義構(gòu)造方法。因此,以下分別介紹下構(gòu)造方法和自定義構(gòu)造方法的實現(xiàn)。
#import <Foundation/Foundation.h> #import "Iphone.h" int main(int argc, const charchar * argv[]) { /* Iphone * phone1 = [Iphone new]; phone1->_cpu = 1.5; phone1->_ram = 512; */ /*Iphone * phone = [Iphone alloc];//offcc phone = [phone init];//[0ffcc init]; */ //開辟內(nèi)存空間,以及初始化成員變量合并一起調(diào)用 Iphone * phone = [[Iphone alloc]init];//[0ffcc init]; phone->_ram = 512; NSLog(@"%@",phone); Iphone * phone2 = [[Iphone alloc] initWithIphoneSize:IphoneSize4point0]; NSLog(@"%@",phone2); Iphone * phone3 = [[Iphone alloc] initWithIphoneSize:IphoneSize4point0 andPhoneColor:IphoneColorBlack]; return 0; }
#import <Foundation/Foundation.h> enum IphoneSize { IphoneSize3point5,//3.5寸屏幕 IphoneSize4point0,//4.0寸屏幕 IphoneSize4point7,//4.7寸屏幕 IphoneSize5point5 //5.5寸屏幕 }; typedef enum IphoneSize IphoneSize; enum IphoneColor { IphoneColorWhite, IphoneColorBlack }; typedef enum IphoneColor IphoneColor; enum IphoneFlashLightStatus { IphoneFlashLightStatusOpen, IphoneFlashLightStatusClose, IphoneFlashLightStatusAuto }; typedef enum IphoneFlashLightStatus IphoneFlashLightStatus; @interface Iphone : NSObject { @public /** 用來存儲iPhone屏幕尺寸 */ //enum IphoneSize 與IphoneSize 等價 IphoneSize _size;//用來存儲iPhone屏幕尺寸 /** 用來存儲iPhone顏色 */ IphoneColor _color;//用來存儲iPhone顏色 /** 用來存儲cpu大小 */ float _cpu; /** 用來存儲內(nèi)部容量大小 */ float _ram; } /**打開閃光燈*/ -(void)openFlashLight; /**關(guān)閉閃光燈*/ -(void)closeFlashLight; /**自動*/ -(void)flaseLightAuto; /**拍照*/ -(void) cameraWithFlashLightStatus:(IphoneFlashLightStatus)flaseLightStatus; /**根據(jù)傳入?yún)?shù)返回相應(yīng)顏色*/ -(NSString * )getColorWithIphoneColor:(IphoneColor)iphoneColor; +(NSString *)getColorWithIphoneColor:(IphoneColor)iphoneColor; //自定義構(gòu)造方法 //1.一定是對象方法 //2.構(gòu)造方法一定是init開頭 -(Iphone *)initWithIphoneSize:(IphoneSize)iphoneSize; -(Iphone *)initWithIphoneSize:(IphoneSize)iphoneSize andPhoneColor:(IphoneColor)iphoneColor; @end通過以上介紹,希望大家對構(gòu)造方法和自定義構(gòu)造方法有所認(rèn)識與區(qū)別,希望對大家有所幫助。
相關(guān)文章
利用iOS開發(fā)實現(xiàn)翻轉(zhuǎn)撲克牌動畫的方法
這篇文章主要給大家介紹了關(guān)于利用iOS開發(fā)實現(xiàn)翻撲克牌動畫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07MacOS無法掛載NFS Operation not permitted錯誤解決辦法
這篇文章主要介紹了MacOS無法掛載NFS Operation not permitted錯誤解決辦法的相關(guān)資料2017-02-02iOS App設(shè)計模式開發(fā)中策略模式的實現(xiàn)示例
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中策略模式的實現(xiàn)示例,例子采用傳統(tǒng)的Objective-C語言編寫,需要的朋友可以參考下2016-03-03IOS 關(guān)鍵字const 、static、extern詳解
這篇文章主要介紹了IOS 關(guān)鍵字const 、static、extern詳解的相關(guān)資料,這里對關(guān)鍵字如何使用,及在IOS開發(fā)中的意義做了詳解,需要的朋友可以參考下2016-11-11Objective-C實現(xiàn)無限循環(huán)輪播器
這篇文章主要介紹了Objective-C實現(xiàn)無限循環(huán)輪播器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05