关于这道题,为何我这样写,什么结果都没有,求解释!!! 设计一个菜单类
*一个添加菜单的对象方法
*一个删除菜单的对象方法
*还有一个打印数组中所有菜单的对象方法
main.m - #import <Foundation/Foundation.h>
- #import "CZCarte.h"
- #import "CZFood.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- CZCarte *carte = [CZCarte new];
- CZFood *f1 = [CZFood new];
- f1.name = @"酸辣土豆丝";
- f1.price = 12;
- f1.info =@"辣椒炒土豆";
- [carte addFood:f1];
- [carte printCarte];
- }
- return 0;
- }
复制代码 菜单类
CZCarte.h- #import <Foundation/Foundation.h>
- @class CZFood;
- @interface CZCarte : NSObject
- {
- NSMutableArray * _foods;
- }
- /**添加菜*/
- -(void) addFood:(CZFood *) food;
- /**删除菜*/
- -(void) removeFood:(CZFood *) food;
- /**打印菜单*/
- -(void) printCarte;
- @end
复制代码
菜单类实现
CZCarte.m
- #import "CZCarte.h"
- #import "CZFood.h"
- @implementation CZCarte
- /**添加菜*/
- -(void) addFood:(CZFood *) food {
-
- [_foods addObject:food];
-
- }
- /**删除菜*/
- -(void) removeFood:(CZFood *) food {
-
- [_foods removeObject:food];
-
- }
- /**打印菜单*/
- -(void) printCarte {
-
- for (CZFood *food in _foods) {
-
- NSLog(@"菜名:%@",[food name]);
-
- NSLog(@"价格:%f",[food price]);
-
- NSLog(@"简介:%@",[food info]);
-
- }
- }
- @end
复制代码
菜类
菜类里没有实现,只有声明
CZFood.h
- #import <Foundation/Foundation.h>
- @interface CZFood : NSObject
- @property (nonatomic,strong) NSString *name; // 菜名
- @property (nonatomic,assign) float price; //价格
- @property (nonatomic,strong) NSString *info; // 简介
- @end
复制代码
|