Design and Implementation of Electronic menus for IOS development (inheritance, encapsulation, and polymorphism)
//// Main. m // electronic menu // # import
# Import Food. h # import CoolFood. h # import HotFood. h # import MainFood. h # import Drinks. h # import BillBoard. hint main (int argc, const char * argv []) {Food * food = [[Food alloc] init]; [food showInformation: @ braised pork andShowMaterial: @ pork ginger garlic andShowNurition: @ calcium, iron, and zinc new vitamins]; NSLog (% @, food); @ autoreleasepool {// insert code here... NSLog (@ Hello, World !); } Return 0 ;}
/// Menu. h // electronic Menu // # import
# 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 order quantity */int _ recordOfFood;/** used to record the order status */BOOL _ statusOfFood; /** used to record Food prices */int _ priceOfFood;}-(void) showInformation :( food *) Food; @ end
//// Menu. m // electronic 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 /// * you can use the inherited electronic menu Implementation 1. Food category */# import
@ Interface Food: NSObject {/** is used to record the Dish name */NSString * _ foodName;/** composition material */NSString * _ material; /** nutrition information */NSString * _ nutrition;/** used to record the food type */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 // electronic 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 selected the dish);} @ end
#import Food.h@interface HotFood : Food-(void)show;@end
# Import HotFood. h @ implementation HotFood-(void) show {NSLog (@ you selected hot dishes);} @ end
#import Food.h@interface MainFood : Food-(void)show;@end
# Import MainFood. h @ implementation MainFood-(void) show {NSLog (@ you selected hot dishes);} @ end
#import Food.h@interface Drinks : Food-(void)show;@end
# Import Drinks. h @ implementation Drinks-(void) show {NSLog (@ you selected liquor);} @ end
# Import
@ Interface BillBoard: NSObject {/** ranking */int _ rank;/** comment */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 ranking is % d, _ rank) ;}@ end