Confusion of objects / classes?

During the game, I noticed that both objects below when sending "init" print the same message "_init: TireSnow" to the console. Can anyone shed some light on why this is happening?

// INTERFACE @interface TireBasic : NSObject { } @end @interface TireSnow : TireBasic { } @end // IMPLEMENT @implementation TireBasic - (id) init { self = [super init]; if(self) { NSLog(@"TB_init: %@", NSStringFromClass([self class])); } return self; } - (NSString *) description { return @"This is a BASIC TIRE."; } @end @implementation TireSnow - (id) init { self = [super init]; if(self) { NSLog(@"TS_init: %@", NSStringFromClass([self class])); } return self; } - (NSString *) description { return @"This is a SNOW TIRE."; } @end 

EDIT2 - added all major ()

 #import <Foundation/Foundation.h> #import "CarParts.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int tireCount; CarClass *newCar_001; EngineClass *newEngine_001; TireBasic *newTire_BASIC; TireSnow *newTire_SNOW; // Setup NSLog(@"COMPOSITION Start"); newCar_001 = [[CarClass alloc] init]; // Engine newEngine_001 = [[EngineClass alloc] init]; [newCar_001 setEngine: newEngine_001]; // TIRES (BASIC) for(tireCount=0; tireCount<2; tireCount++) { newTire_BASIC = [[TireBasic alloc] init]; [newCar_001 setTire:newTire_BASIC]; [newTire_BASIC release]; } // TIRES (SNOW) for(tireCount=0; tireCount<2; tireCount++) { newTire_SNOW = [[TireSnow alloc] init]; [newCar_001 setTire:newTire_SNOW]; [newTire_SNOW release]; } // Display [newCar_001 printCar]; // Clean up [newCar_001 release]; [newEngine_001 release]; [pool drain]; return 0; } 

OUTPUT

 > COMPOSITION Start > _init: CarClass > _init: EngineClass > TB_init: TireBasic > TB_init: TireBasic > TB_init: TireSnow ***** > TS_init: TireSnow > TB_init: TireSnow ***** > TS_init: TireSnow > > This is a BASIC TIRE. > This is a BASIC TIRE. > This is a SNOW TIRE. > This is a SNOW TIRE. > > _deal: CarClass > TB_deal: TireBasic > TB_deal: TireBasic > TS_deal: TireSnow > TB_deal: TireSnow ****** > TS_deal: TireSnow > TB_deal: TireSnow ****** > _deal: EngineClass 

The line with the stars comes from TireSnow [super init] and [super dealloc], it seems [self class], which returns β€œTireSnow” every time, can anyone explain why?

thank you very much

Gary

0
source share
3 answers
 > TS_deal: TireSnow > TB_deal: TireSnow 

You ask why TireSnow is printed twice?

First [TireBasic init] is executed, then [TireSnow init] . In both cases, they print the class name, and in both cases the class name is TireSnow .

In other words, [self class] will always return the same object, no matter what method it executes.

+3
source

TireSnow will write its class twice, once in TireSnow init and once from a super call, in TireBasic init.

Just to check: maybe you are confusing two magazines from TireSnow, being from TireBasic and TireSnow?

+1
source

Can we see your [[_____ alloc] init] expression? I do not see how

[[TireSnow alloc] init] and [[TireBasic alloc] init] will return the same. Well, maybe if:

 TireSnow *tire; tire = (TireSnow *) [TireBasic alloc]; tire = [tire init]; 
0
source

Source: https://habr.com/ru/post/948668/


All Articles