构造方法,也就是int方法,不接受任何的参数,而在实际的开发过程中,为了方便,会经常自定义构造方法。因此,以下分别介绍下构造方法和自定义构造方法的实现。
#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;
}
通过以上介绍,希望大家对构造方法和自定义构造方法有所认识与区别,希望对大家有所帮助。#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