如何继承多次继承基类的类

时间:2014-04-16 08:34:29

标签: class tcl itcl

标题不清楚。我在这里解释

我有一个包裹说package provide test。它有classes。我正在使用Itcl。包具有以下结构

::itcl::class classA {
written something having constructor and methods
}

::itcl::class classB {
inherit ::test::classA
having its own constructor and methods
}

::itcl::class classC {
inherit ::test::classA
having its own constructor and methods
}

::itcl::class classD {
inehrit ::test::classB ::test::classC
having its own constructor and methods
}

当我要求package test时,我收到的错误

class "::test::classD" inherits base class "::test::classA" more than once:

我该如何处理错误

1 个答案:

答案 0 :(得分:1)

由于路径模糊,不允许使用钻石继承。 即

      TopClass
      /       \
LeftClass   RightClass
      \       /
     BottomClass

作为解决方法,您可以使用合成(has-a)而不是继承 (IS-A)。

::itcl::class classA {
}

::itcl::class classB {
    inherit ::test::classA
}

::itcl::class classC {
    inherit ::test::classA
}

::itcl::class classD {
   constructor {} {
     set _b [::test::classB #auto]
     set _c [::test::classC #auto]
   }
   destructor {
     ::itcl::delete $_b
     ::itcl::delete $_c
   }
   private {
      variable _b ""
      variable _c ""
   }
}

现在在classD中,您必须特定于代码到达的classB或classC路径。