如何将一个类的itcl对象传递给另一个类的对象并使用传递的对象的函数?

时间:2014-03-27 11:01:49

标签: oop tcl itcl

在namespace :: NodeTypes中,我有一个NodeType1类和另一个NodeType2类的另一个对象。

我还有一个类NetworkCollector和另一个NodeType2类的另一个对象

我需要将NodeType1和NodeType2的对象作为NetworkCollector对象中的参数传递,并使用NetworkCollector类中传递的对象的函数。

您能否建议一些可能的解决方案,如何将一个对象的引用传递给另一个类中的另一个方法。 以下示例代码不起作用:

itcl::class ::NodeTypes::NodeType1 {
  private activeChannels 0;
  method addChannels {numChannels} {
      #set active channels
      set activeChannels $numChannels
  }   
      method getActiveChannels {} {
        # return number of channels being used in the NodeType1 object
        return $activeChannels
      }
}

itcl::class ::NodeTypes::NodeType2 {
  private activeChannels 0;
  method addChannels {numChannels} {
      #set active channels
      set activeChannels $numChannels
  }
  method getActiveChannels {} {
      # return number of channels being used in the NodeType2 object
      return $activeChannels
  }
}

::NodeTypes::NodeType2 NodeType2Object2

itcl::class ::NodeTypes:NetworkCollector {
  method getTotalUsedChannels {Node1 Node2} {
      set Node1Channels [Node1 getActiveChannels] 
      set Node2Channels [Node2 getActiveChannels] 
      set totalActiveChannels [$Node1Channels + $Node2Channels]
      puts "Total active channels are $totalActiveChannels"
  }   
}

#creating objects
::NodeTypes::NodeType1 NodeType1Object
::NodeTypes::NodeType2 NodeType2Object

#adding channels to nodes
NodeType1Object::addChannels 5 
NodeType2Object::addChannels 10

#calculating total active channels
::NodeTypes:NetworkCollector networkCollector1

networkCollector1 getTotalUsedChannels $NodeType1Object $NodeType2Object

问题:抛出错误----->无法读取“NodeType1Object”:没有这样的变量

您能否建议一些可能的解决方案,如何将一个对象的引用传递给另一个类中的另一个方法。

3 个答案:

答案 0 :(得分:1)

愚蠢的事情是......你的代码几乎正确(据我所见)。您只需要知道在Tcl中添加数字是使用expr命令完成的,并且当您阅读它们时,总是必须明确地取消引用这些变量(即,$foo意味着“读取值”变量foo“):

set Node1Channels [$Node1 getActiveChannels] 
set Node2Channels [$Node2 getActiveChannels] 
set totalActiveChannels [expr {$Node1Channels + $Node2Channels}]

您也可以编写如下方法:

method getTotalUsedChannels {Node1 Node2} {
    set numChans [$Node1 getActiveChannels] 
    incr numChans [$Node2 getActiveChannels] 
    puts "Total active channels are $numChans"
}

答案 1 :(得分:1)

问题1

除了Donal所说的,因为getTotalUsedChannels是直接从一个类调用的,而不是任何对象的一部分,所以你应该把它变成public proc

public proc getTotalUsedChannels ....

而不是

method getTotalUsedChannels ....

像这样调用它:

::NodeTypes:NetworkCollector::getTotalUsedChannels NodeType1Object NodeType2Object

第2期

而不是:

private activeChannels 0;

您需要关键字variable

private variable activeChannels 0

答案 2 :(得分:0)

好的..解决方案很简单..在proc中传递的对象中添加美元符号$ - 感谢Donal 即

方法getTotalUsedChannels {Node1 Node2} {

设置Node1Channels [$ Node1 getActiveChannels]

设置Node2Channels [$ Node2 getActiveChannels] ...

packa r Itcl
namespace eval NodeTypes {}
itcl::delete class NodeTypes::NodeType1
itcl::delete class NodeTypes::NodeType2
itcl::delete class NodeTypes::NetworkCollector
itcl::delete class NodeTypes::NodeBase


itcl::class NodeTypes::NodeType1 { 
  private variable activeChannels 0;

  method addChannels {numChannels} {
      #set active channels
      set activeChannels $numChannels
  }

  method getActiveChannels {} {
      # return number of channels being used in the NodeType1 object
      return $activeChannels
    }
}

itcl::class NodeTypes::NodeType2 { 
  private variable activeChannels 0;
  method addChannels {numChannels} {
      #set active channels
      set activeChannels $numChannels
  }
  method getActiveChannels {} {
      # return number of channels being used in the NodeType2 object
      return $activeChannels
  }
}


itcl::class NodeTypes::NetworkCollector {
  method getTotalUsedChannels {Node1 Node2} {
      set Node1Channels [$Node1 getActiveChannels] 
      set Node2Channels [$Node2 getActiveChannels] 
      set totalActiveChannels [expr $Node1Channels + $Node2Channels]
      puts "Total active channels are $totalActiveChannels"
  }   
}

#creating objects
NodeTypes::NodeType1 NodeType1Object
NodeTypes::NodeType2 NodeType2Object

#adding channels to nodes
NodeType1Object addChannels 9
NodeType2Object addChannels 10

#calculating total active channels
NodeTypes::NetworkCollector networkCollector1

networkCollector1 getTotalUsedChannels NodeType1Object NodeType2Object