gpt4 book ai didi

oop - 用于将自定义对象作为参数传递的 OCaml 语法

转载 作者:行者123 更新时间:2023-12-03 22:54:53 25 4
gpt4 key购买 nike

我正在尝试掌握 OCaml 语言语法,但在应用某些 OOP 结构时遇到了一些麻烦。我使用以下代码的目标是拥有一个继承自虚拟类 foo 的类栏。虚拟类包含三个虚拟方法,我希望将“玩家”对象的实例作为参数。当我编译以下代码时,出现错误:方法 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;;

最佳答案

(我不知道 OCaml,但我知道 F#,它们很相似,所以希望我猜对了。)

尝试改变

method virtual doThis : player 
method virtual doThat : player
method virtual notifyAll : player array


method virtual doThis : player -> unit
method virtual doThat : player -> unit
method virtual notifyAll : player array -> unit

关于oop - 用于将自定义对象作为参数传递的 OCaml 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/501731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com