2

在命名空间 ::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”:没有这样的变量

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

4

3 回答 3

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"
}
于 2014-03-27T13:19:12.440 回答
1

第一期

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

public proc getTotalUsedChannels ....

代替

method getTotalUsedChannels ....

像这样调用它:

::NodeTypes:NetworkCollector::getTotalUsedChannels NodeType1Object NodeType2Object

第 2 期

代替:

private activeChannels 0;

您需要关键字variable

private variable activeChannels 0
于 2014-03-27T21:31:18.000 回答
1

好的 .. 解决方案很简单 .. 在 proc 中通过 ref 传递的对象中添加美元符号 $ - 谢谢 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
于 2014-03-28T11:02:16.723 回答