IOSの構造方法とカスタム構造方法の違いと実現
構造方法、つまり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
以上の紹介を通して、構造方法とカスタム構造方法について認識と違いがあります。