当前位置:首页 > 通信资讯 > 正文

ios 类的结构(ios 结构体)

构造方法,也就是int方法,不接受任何的参数,而在实际的开发过程中,为了方便,会经常自定义构造方法。因此,以下分别介绍下构造方法和自定义构造方法的实现。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #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]; */ //开辟内存空间,以及初始化成员变量合并一起调用 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; }

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #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; /** 用来存储内部容量大小 */ float _ram; } /**打开闪光灯*/ -(void)openFlashLight; /**关闭闪光灯*/ -(void)closeFlashLight; /**自动*/ -(void)flaseLightAuto; /**拍照*/ -(void) cameraWithFlashLightStatus:(IphoneFlashLightStatus)flaseLightStatus; /**根据传入参数返回相应颜色*/ -(NSString * )getColorWithIphoneColor:(IphoneColor)iphoneColor; +(NSString *)getColorWithIphoneColor:(IphoneColor)iphoneColor; //自定义构造方法 //1.一定是对象方法 //2.构造方法一定是init开头 -(Iphone *)initWithIphoneSize:(IphoneSize)iphoneSize; -(Iphone *)initWithIphoneSize:(IphoneSize)iphoneSize andPhoneColor:(IphoneColor)iphoneColor; @end

通过以上介绍,希望大家对构造方法和自定义构造方法有所认识与区别,希望对大家有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。