我試圖通過使用編譯代碼而不是頂層來學習OCaml;然而,網上的大部分示例代碼似乎都吸引了後者。在OCaml中即時創建對象
我想創建一個新的Foo內每個對象的方法下面。此代碼不會編譯,引用doFooProc定義的語法錯誤。
class bar =
object (self)
method doFooProc = (new Foo "test")#process
end;;
class foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end;;
此外,「let」語法在類定義中似乎不友好。這是爲什麼?
class bar =
object (self)
method doFooProc =
let xxx = (new Foo "test");
xxx#process
end;;
class foo (param1:string)=
object (self)
method process = Printf.printf "%s\n" "Processing!"
initializer Printf.printf "Initializing with param = %s\n" param1
end;;
我如何去在doFooProc方法創建類Foo的一個新對象,並調用實例化Foo的進程的命令?
你說得對。我養成了對其他語言的類名使用大寫字母的習慣,並繼續使用OCaml進行恢復。謝謝! – 2009-02-04 18:43:01
呃。只是要清楚。你可以在引用模塊和其他文件時採取這種直覺(因爲它們是隱式模塊) – nlucaroni 2009-02-04 18:44:53