构造方法,也就是int方法,不接受任何的参数,而在实际的开发过程中,为了方便,会经常自定义构造方法。因此,以下分别介绍下构造方法和自定义构造方法的实现。
#import#import "Iphone.h" int main(int argc, const charchar * argv[]) { //开辟内存空间,以及初始化成员变量合并一起调用 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通过以上介绍,希望大家对构造方法和自定义构造方法有所认识与区别,希望对大家有所帮助。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 //enum IphoneSize 与IphoneSize 等价 IphoneSize _size;//用来存储iPhone屏幕尺寸 IphoneColor _color;//用来存储iPhone颜色 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



