objective-c 入门:类的复合

  1 //
  2 //  main.m
  3 //  TEXT2 car
  4 //
  5 //  Created by ponytai1 on 16/11/4.
  6 //  Copyright © 2016年 ponytai1. All rights reserved.
  7 //
  8 
  9 #import<Foundation/Foundation.h>
 10 @interface Tire:NSObject
 11 {
 12     
 13 }
 14 @end  //tire interface
 15 
 16 
 17 @implementation Tire
 18 -(NSString *)description
 19 {
 20     return (@"i am a tire");
 21 }
 22 @end    //tire implementation
 23 
 24 
 25 @interface Engine:NSObject
 26 {
 27     
 28 }
 29 @end    //endine interface
 30 
 31 
 32 @implementation Engine
 33 -(NSString*)description
 34 {
 35     return (@"i am a engine");
 36 }
 37 @end       //engine implementation
 38 
 39 
 40 @interface V8engine:Engine
 41 @end    //v8engine interface
 42 
 43 
 44 @implementation V8engine
 45 -(NSString *) description
 46 {
 47     return(@"i am a better engine");
 48 }
 49 @end     //v8engine implementation
 50 
 51 
 52 @interface BetterTire:Tire
 53 @end   //BetterTire interface
 54 
 55 
 56 @implementation BetterTire
 57 -(NSString *) description
 58 {
 59     return(@"i am a better tire");
 60 }
 61 @end       //BetterTire interface
 62 
 63 
 64 @interface Car:NSObject
 65 {
 66     Tire *tires[4];
 67     Engine *engine;
 68 }
 69 -(void)print;
 70 -(void)setTire:(Tire *)tire atIndex:(int) index;
 71 -(Tire *)tireAtIndex:(int)index;
 72 -(void)setEngine:(Engine *)newEngine;
 73 -(Engine *)engine;
 74 
 75 @end      //car interface
 76 
 77 
 78 @implementation Car
 79 -(id)init
 80 {
 81     if(self = [super init])
 82     {
 83         engine = [Engine new];
 84         tires[0] = [Tire new];
 85         tires[1] = [Tire new];
 86         tires[2] = [Tire new];
 87         tires[3] = [Tire new];
 88     }
 89     return self;
 90 }
 91 -(void)setTire:(Tire *)tire atIndex:(int)index
 92 {
 93     if(index > 3 ||index < 0)
 94     {
 95         NSLog(@"index is error");
 96         exit(1);
 97     }
 98     tires[index] = tire;
 99 }
100 -(void)setEngine:(Engine *)newEngine
101 {
102     engine = newEngine;
103 }
104 -(Tire *)tireAtIndex:(int)index
105 {
106     if(index > 3|| index < 0)
107     {
108         NSLog(@"index is error");
109         exit(1);
110     }
111     return tires[index];
112 }
113 -(Engine *)engine
114 {
115     return engine;
116 }
117 -(void)print
118 {
119     NSLog(@"%@",engine);
120     NSLog(@"%@",tires[0]);
121     NSLog(@"%@",tires[1]);
122     NSLog(@"%@",tires[2]);
123     NSLog(@"%@",tires[3]);
124 }
125 @end    //car implementation
126 
127 
128 int main(int argc,const char* argv[])
129 {
130     Car *car;
131     car = [Car new];
132     Engine *engine = [Engine new];
133     [car setEngine:(Engine *)engine];
134     for(int i = 0;i < 4;i++)
135     {
136         Tire *tire = [Tire new];
137         [car setTire:(Tire *) tire atIndex:(int) i];
138     }
139     NSLog(@"Before update car");
140     [car print];
141     Engine *newengine = [V8engine new];
142     [car setEngine:(Engine *)newengine];
143     Tire *newtire;
144     newtire = [BetterTire new];
145     [car setTire:(Tire *)newtire atIndex:(int) 2];
146     NSLog(@"After update car");
147     [car print];
148     return 0;
149 }
原文地址:https://www.cnblogs.com/Ponytai1/p/6030758.html