我正在尝试掌握 OCaml 语言语法,但在应用一些 OOP 结构时遇到了一些麻烦。我使用以下代码的目标是拥有一个继承自虚拟类 foo 的类 bar。虚拟类包含三个虚拟方法,我希望将“玩家”对象的实例作为参数。当我编译下面的代码时,我得到了错误:方法 doThis 有类型 'a -> 'b 但应该有类型 player 。这是什么意思(记住,我是新手),我该如何纠正它?
谢谢!
class player =
object
end;;
class virtual foo =
object (self)
method virtual doThis : player
method virtual doThat : player
method virtual notifyAll : player array
end;;
class bar (playersIn: player array) =
object (self)
inherit foo
method doThis (p:player) = Printf.printf "%s\n" "This!"
method doThat (p:player) = Printf.printf "%s\n" "That!"
method notifyAll (p:player array) = Printf.printf "%s\n" "Notifying!"
end;;