在嘗試以前我單身基於全局控制器類轉換爲通過所需的方法從一個對象到另一個需要他們更多的OOP友好的依賴注入方法的過程。我碰到的地方我以前的類其init過程中使用的全局對象的問題。所需參數在Objective-C的構造
(id)init
{
self = [super init];
if (self)
{
[self setUpPhysicsWithWorld:FMPresenter.physics.world];
}
return self;
}
其中FMPresenter.physics
返回一個單體物理對象。因爲我的對象不能正確地沒有物理對象實例化,以init
呼叫無效。我看到這個被使用:
(id) init
{
NSAssert(NO, @"init not allowed");
[self release];
return nil;
}
(id) initWithPhysics:(FMPhysics*)physics
{
self = [super init];
if (self) {
[self setUpPhysicsWithWorld:physics.world];
}
return self;
}
這是在Objective-C中執行構造函數參數的首選方法嗎?
謝謝,我不太確定,因爲我想起了Java的沒讓你調用基類的構造除非您指定,或者根本沒有指定任何構造函數。 – 2012-02-13 12:54:06