CarParts3
来源:互联网 发布:xbox360淘宝 编辑:程序博客网 时间:2024/05/19 00:42
新建工程:CarParts3,修改上文代码:
//// main.m// CarParts//// Created by cloud on 13-3-8.// Copyright (c) 2013年 cloud. All rights reserved.//#import <Foundation/Foundation.h>@interface Tire:NSObject@end@implementation Tire- (NSString *) description{ return (@"I am a tire.I last a while");}//description@end@interface Engine : NSObject@end@implementation Engine- (NSString *)description{ return (@"I am an engine.Vroom!");}@end@interface Slant6 : Engine@end@implementation Slant6- (NSString *) description{ return (@"I am a slant-6.VROOM!");}@end@interface AllWeatherRadial : Tire@end@implementation AllWeatherRadial- (NSString *) description{ return (@"I am a tire for rain or shine.");}@end@interface Car : NSObject{ Engine *engine; Tire *tires[4];}- (Engine *) engine;- (void) SetEngine: (Engine *) newEngine;- (Tire *) tireAtIndex: (int) index;- (void) setTire:(Tire *) tire atIndex:(int) index;- (void) print;@end@implementation Car//- (id)init//{ //if (self=[super init]) { //engine=[Engine new]; //tires[0]=[Tire new]; //tires[1]=[Tire new]; //tires[2]=[Tire new]; //tires[3]=[Tire new]; //}// return (self);//}- (Engine *) engine{ return (engine);}- (void) SetEngine:(Engine *)newEngine{ engine=newEngine;}- (void) setTire:(Tire *)tire atIndex:(int)index{ if (index<0||index>3) { NSLog(@"bad index (%d) in setTire:atIndex;",index); exit(1); } tires[index] = tire;}-(Tire *)tireAtIndex:(int)index{ if(index<0 || index >3) { NSLog(@"bad index (%d) in tireAtIndex;",index); exit(1); } return (tires[index]);}- (void) print{ NSLog(@"%@",engine); NSLog(@"%@",tires[0]); NSLog(@"%@",tires[1]); NSLog(@"%@",tires[2]); NSLog(@"%@",tires[3]);}@endint main(int argc, const char * argv[]){ @autoreleasepool { // insert code here... Car *car; car=[Car new]; Engine *engine=[Slant6 new]; [car SetEngine:engine]; int i; for(i=0;i<4;i++) { Tire *tire=[AllWeatherRadial new]; [car setTire:tire atIndex:i]; } [car print]; } return 0;}
运行结果如下:
为car对象,赋予了新的engine和tire对象,slant6和AllWeatherRadial。slant6从engine类派生。AllWeatherRadial从Tire类派生。
- CarParts3
- android 3.1以上boot complete receiver接受的问题
- CSP平台银联前置系统ATM他带本和本代他交易失败原因分析
- 11198 - Dancing Digits
- hql查询方式 和 criteria 查询
- js中innerHTML用法
- CarParts3
- char,nchar,varchar,nvarchar之间的区别
- mt9d131 驱动简介
- Ubuntu上安装启动SSH Server
- 11.MAP查询每年的世界杯冠军
- 详细介绍ORACLE sqlplus命令
- 一天一个设计模式(2): 外观模式
- Xen 环境配置
- rsync+inotify 配置实时同步服务器