// Implement carparts
// Implement carparts # import <Foundation/Foundation. h> @ interface tire: nsobject @ end @ implementation tire-(nsstring *) Description {return (@ "I Am a tire. I last a while. ");} @ end @ Interface Engine: nsobject @ end @ implementation engine-(nsstring *) Description {return (@" I am an engine. vrooom! ") ;}@ 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-(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 []) {car * car = [Car new]; engine * Engine = [engine new]; [Car setengine: Engine]; for (INT I = 0; I <4; I ++) {tire * tire = [Tire new]; [Car settire: Tire atindex: i];} [Car print]; return 0 ;}
Result:
Objective-C)