标签:继承 多态 ios开发 objective-c 电子菜单的设计
// // main.m // 电子菜单 // #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:@"猪肉 葱姜蒜"andShowNurition:@"钙铁锌新维生素"]; NSLog(@"%@",food); @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }
// // Menu.h // 电子菜单 // #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 { /**用来记录点餐数量*/ int _recordOfFood; /**用来记录点餐状态*/ BOOL _statusOfFood; /**用来记录食品价格*/ int _priceOfFood; } -(void)showInformation:(Food *)food; @end
// // Menu.m // 电子菜单 // #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 // 电子菜单 // /* 可以使用继承 电子菜单的实现 1.食品类 */ #import <Foundation/Foundation.h> @interface Food : NSObject { /**用来记录菜品名称*/ NSString * _foodName; /**组成材料*/ NSString * _material; /**营养信息*/ NSString * _nutrition; /**用来记录食品种类*/ 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 // 电子菜单 // #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:@"食品名称:%@食材:%@ 营养物质:%@",_foodName,_material,_nutrition]; } @end
#import "Food.h" @interface CoolFood : Food -(void)show; @end
#import "CoolFood.h" @implementation CoolFood -(void)show { NSLog(@"您选择的是凉菜"); } @end
#import "Food.h" @interface HotFood : Food -(void)show; @end
#import "HotFood.h" @implementation HotFood -(void)show { NSLog(@"您选择的是热菜"); } @end
#import "Food.h" @interface MainFood : Food -(void)show; @end
#import "MainFood.h" @implementation MainFood -(void)show { NSLog(@"您选择的是热菜"); } @end
#import "Food.h" @interface Drinks : Food -(void)show; @end
#import "Drinks.h" @implementation Drinks -(void)show { NSLog(@"您选择的是酒水"); } @end
#import <Foundation/Foundation.h> @interface BillBoard : NSObject { /**排名*/ int _rank; /**评论*/ 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(@"您的排名是%d",_rank); } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:继承 多态 ios开发 objective-c 电子菜单的设计
原文地址:http://blog.csdn.net/u012701023/article/details/46980321