main.m// E-menu//#import <Foundation/Foundation.h> #import "Food.h" #import "CoolFood.h" #import " HotFood.h "#import" MainFood.h "#import" Drinks.h "#import" BillBoard.h "int main (int argc, const char * argv[]) {Food * food = [[Food alloc]init]; [Food showinformation:@ "andshowmaterial:@" pork onion ginger garlic "andshownurition:@" calcium iron Zinc new vitamin "]; NSLog (@ "%@", food); @autoreleasepool { //Insert code here ... NSLog (@ "Hello, world!"); } return 0;}
menu.h// E-menu//#import <Foundation/Foundation.h> #import "Food.h" #import "CoolFood.h" #import " HotFood.h "#import" MainFood.h "#import" Drinks.h "#import" BillBoard.h "@interface menu:nsobject{ /** used to record the number of order meals * * int _recordoffood; /** is used to record order status */ BOOL _statusoffood; /** used to record food prices */ int _priceoffood;} -(void) Showinformation: (food *) food; @end
menu.m// E-menu//#import "Menu.h" @implementation menu-(void) Showinformation: (Food *) food{ if ([ Food Iskindofclass:[coolfood class]) { Coolfood * cool = (Coolfood *) food; [Cool show]; } else if ([Food Iskindofclass:[hotfood class]]) { Hotfood * hot = (Hotfood *) food; [Hot show]; } else if ([Food Iskindofclass:[mainfood class]]) { Mainfood * Mainfood = (Mainfood *) food; [Mainfood show]; } Else { drinks * drinks = (drinks *) food; [Drinks show];} } @end
food.h// Electronic menu///* can be implemented using inherited electronic menus 1. Food class */#import <foundation/foundation.h>@ Interface food:nsobject{ /** used to record the name of the dish * * * NSString * _foodname; /** Composition Material */NSString * _material; /** Nutrition Information * * NSString * _nutrition; /** used to record food types * * NSString * _kindoffood;} @property NSString * foodname; @property nsstring * material; @property nsstring * NUTRITION; @property NSString * Kindoffood ;-(void) Showinformation: (nsstring*) foodname andshowmaterial: (NSString *) material andshownurition: (NSString *) Nutrition; @end
food.m// E-menu//#import "Food.h" @implementation food@synthesize foodname = _foodname; @synthesize material = _material; @synthesize nutrition = _nutrition; @synthesize kindoffood = _kindoffood;-(void) Showinformation: ( nsstring*) foodname andshowmaterial: (NSString *) material andshownurition: (NSString *) nutrition; { _foodname = foodname; _material = material; _nutrition = nutrition; NSLog (@ "%@%@%@", _foodname,_material,_nutrition);} -(NSString *) description{ return [nsstring stringwithformat:@ "Food Name:%@ Ingredients:%@ Nutrients:%@", _foodname,_material,_ Nutrition];} @end
#import "Food.h" @interface coolfood:food-(void) show; @end
#import "CoolFood.h" @implementation coolfood-(void) show{ NSLog (@ "You chose a cold dish");} @end
#import "Food.h" @interface hotfood:food-(void) show; @end
#import "HotFood.h" @implementation hotfood-(void) show{ NSLog (@ "You chose a hot dish");} @end
#import "Food.h" @interface mainfood:food-(void) show; @end
#import "MainFood.h" @implementation mainfood-(void) show{ NSLog (@ "You chose a hot dish");} @end
#import "Food.h" @interface drinks:food-(void) show; @end
#import "Drinks.h" @implementation drinks-(void) show { NSLog (@ "Your choice is Wine");} @end
#import <Foundation/Foundation.h> @interface billboard:nsobject{ /** ranking */ int _rank; /** comments */ NSString * _comment;} -(ID) init;-(void) Showrank: (int) rank; @end
#import "BillBoard.h" @implementation billboard-(ID) init{self = [super init]; if (self!=nil) { _rank = 0; } return self;} -(void) Showrank: (int) rank{ _rank + = rank; NSLog (@ "Your rank is%d", _rank);} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design and implementation of electronic menus for iOS development (inheritance, encapsulation, polymorphism)