Ocaml如何从另一个类

时间:2015-05-28 16:47:06

标签: class object ocaml

我与对象和课程有一些混乱。我试图从课堂上的班级小伙子那里调用一种方法,在家里随时添加一个孩子,但我得到一个错误,我不会安静地理解。

class virtual person name lastName sex =
    object
        val name = (name:string)
        val mutable lastName = (lastName:string)
        val sex = (sex:char)
    end;;

class man name lastName=
    object
        inherit person name lastName 'm'
        method printNameAndLastName = (name ^ " " ^ lastName)
        method changeLastName newLastName = lastName <- newLastName
        method printLastName = lastName
end;;

class woman name lastName =
    object
        inherit person name lastName 'w'
        method printNameAndLastName = (name ^ " " ^ lastName)
        method changeLastName newLastName = lastName <- newLastName
        method printLastName = lastName
end;;

class child name sex=
    object
        inherit person name "" 'm'
        method printNameAndLastName = (name ^ " " ^ lastName)
        method changeLastName newLastName = lastName <- newLastName
        method printLastName = lastName
end;;

class family familyName man woman=
    object
        val man = man
        val woman = woman
        val mutable kids = ([] :string list)
        initializer woman#changeLastName (man#printLastName)
        method addKid kid = kids <- (kid#printLastName)::kids
        method printFamily = (man#printNameAndLastNAme ^ ", " ^ woman#printNameAndLastName)
end;;



Error: Some type variables are unbound in this type:
         class family :
           'a ->
           (< printLastName : 'c; printNameAndLastNAme : string; .. > as 'b) ->
           (< changeLastName : 'c -> unit; printNameAndLastName : string;
              .. >
            as 'd) ->
           object
             val mutable kids : string list
             val man : 'b
             val woman : 'd
             method addKid : < printLastName : string; .. > -> unit
             method printFamily : string
           end
       The method addKid has type
         (< printLastName : string; .. > as 'e) -> unit
       where 'e is unbound

添加方法addKid时发生错误。也是最后一个方法printFamily,我想在男女名字后加上孩子名字的列表,看起来像这样:

  • (string * string)* string list =((&#34; man&#34;,&#34; woman&#34;),[&#34; kid1&#34 ;; &#34; KID2&#34;。])

我一直在试验这个但是我无法将字符串与列表合并,得到预期的错误(字符串*字符串)*字符串。

谢谢你的时间:)

1 个答案:

答案 0 :(得分:1)

类中所有值的类型应绑定到其定义。例如:

# class ident x = object
    method x = x
  end;;
    Characters 6-11:
  class ident x = object
        ^^^^^
Error: Some type variables are unbound in this type:
         class ident : 'a -> object method x : 'a end
       The method x has type 'a where 'a is unbound

这意味着,您需要将其绑定到具体类型:

class ident x = object
    method x : int = x
  end;;
class ident : int -> object method x : int end

或者将您的类多态和绑定值赋予新引入的类型变量:

# class ['a] ident x = object
    method x : 'a = x
  end;;
class ['a] ident : 'a -> object method x : 'a end

在您的情况下,manwomankid不受约束。因为,每次定义一个类时,都会创建同名的类型,你可以将它们绑定到manwomanchild(并修复方法中的拼写错误)

class family familyName man woman =
    object
        val man : man  = man
        val woman : woman = woman
        val mutable kids = ([] :string list)
        initializer woman#changeLastName (man#printLastName)
        method addKid (kid : child) = kids <- (kid#printLastName)::kids
        method printFamily = (man#printNameAndLastName ^ ", " ^ woman#printNameAndLastName)
end;;