码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发之电子菜单的设计与实现(继承、封装、多态)

时间:2015-07-21 09:10:16      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:继承   多态   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开发之电子菜单的设计与实现(继承、封装、多态)

标签:继承   多态   ios开发   objective-c   电子菜单的设计   

原文地址:http://blog.csdn.net/u012701023/article/details/46980321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!