2017-10-19 180 views
1

我有一個testFactory類。其目的是爲了能夠通過工廠,然後安排結果進行演示。到目前爲止,當試圖在測試方法中調用createProductA時,編譯器會抱怨createProductA是未綁定的(Unbound value createProductA)。OOP - 如何在原因內部調用類方法

什麼是在類中調用方法的正確語法?

class testFactory (factory: abstractFactory) => { 
    as _; 
    pub createProductA => factory#createProductA; 
    pub createProductB => factory#createProductB; 

    pub test() => { 
    Js.log createProductA; 
    Js.log createProductB; 
    } 
}; 
+1

我不知道,如果我之前提到過這一點,但你可能想看看[真實世界的OCaml對類節(https://開頭realworldocaml。組織/ V1/EN/HTML/classes.html)。您可以使用[reason-tools](https://github.com/reasonml/reason-tools)將OCaml代碼翻譯爲Reason。 – glennsl

+0

我確實閱讀過這一章,而且我經常重新閱讀(特別是在地鐵上),並嘗試使用_以及#。可能在衝過來的時候,我忽略了一些東西。我一如既往地感謝。 –

回答

3

這是類定義的as _;部分來自於,如果你曾經想知道那是什麼東西了。

createProductAcreateProductB是方法而不是函數,因此需要在對象上調用它們。 Reason/OCaml不會自動將當前對象綁定到像thisself這樣的名稱,但會將其放在您的手上,這正是as所做的,_的意思是,像往常一樣,「我不關心這個」。因此,如果您將as _;更改爲as self;您可以參考self作爲其他地方的當前對象。

試試這個:

class testFactory (factory: abstractFactory) => { 
    as self; 
    pub createProductA => factory#createProductA; 
    pub createProductB => factory#createProductB; 

    pub test() => { 
    Js.log self#createProductA; 
    Js.log self#createProductB; 
    } 
}; 
相關問題