// // main.m // 電子菜單 // #import#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#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@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@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