Constructor, custom constructor, and custom Constructor
//// Main. m # import <Foundation/Foundation. h> # import "Iphone. h "int main (int argc, const char * argv []) {/* Iphone * phone1 = [Iphone new]; phone1-> _ cpu = 1.5; phone1-> _ ram = 512; * // * Iphone * phone = [Iphone alloc]; // offcc phone = [phone init]; // [0 ffcc init]; * // open up the memory space and combine the initialization member variables to call the Iphone * phone = [[Iphone alloc] init]; // [0 ffcc init]; phone-> _ ram = 512; NSLog (@ "% @", phone); Iphone * phone2 = [[Iphone alloc] initWithIphoneSize: IphoneSize4point0]; NSLog (@ "% @", phone2); Iphone * phone3 = [[Iphone alloc] initWithIphoneSize: IphoneSize4point0 andPhoneColor: IphoneColorBlack]; return 0 ;}
//// Iphone. h # import <Foundation/Foundation. h> enum IphoneSize {IphoneSize3point5, // 3.5 inch screen IphoneSize4point0, // 4.0 inch screen IphoneSize4point7, // 4.7 inch screen IphoneSize5point5 // 5.5 inch screen}; typedef enum IphoneSize; enum IphoneColor {IphoneColorWhite, IphoneColorBlack}; typedef enum IphoneColor; enum IphoneFlashLightStatus {IphoneFlashLightStatusOpen, Shenzhen, IphoneFlashLightStatusAuto}; typedef enum IphoneFlashLightStatus; @ interface Iphone: NSObject {@ public/** is used to store the iPhone screen size * // The iPhone size and the iPhone size are equivalent to the iPhone size _ size; // used to store the iPhone screen size/** used to store the iPhone color */IphoneColor _ color; // used to store the iPhone color/** used to store the cpu size */float _ cpu; /** used to store the internal capacity */float _ ram;}/** turn on the flashlight */-(void) openFlashLight;/** turn off the flashlight */-(void) closeFlashLight;/** auto */-(void) flaseLightAuto;/** photo */-(void) cameraWithFlashLightStatus :( IphoneFlashLightStatus) flaseLightStatus; /** return the corresponding color based on input parameters */-(NSString *) getColorWithIphoneColor :( IphoneColor) iphoneColor; + (NSString *) getColorWithIphoneColor :( IphoneColor) iphoneColor; // custom constructor // 1. it must be an object method // 2. the construction method must start with init-(Iphone *) initWithIphoneSize :( IphoneSize) iphoneSize;-(Iphone *) initWithIphoneSize :( IphoneSize) iphoneSize andPhoneColor :( IphoneColor) iphoneColor; @ end
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.