2014-03-27 46 views
1

在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」:沒有這樣的變量

可否請你提出一些可能的解決方案如何將一個對象的引用傳遞給另一個類中的另一個方法。

回答

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

除了什麼多納爾說,由於getTotalUsedChannels直接從所謂的一類,而不是任何對象的一部分,你應該讓一個public proc:代替

public proc getTotalUsedChannels .... 

method getTotalUsedChannels .... 

調用它像這樣:

::NodeTypes:NetworkCollector::getTotalUsedChannels NodeType1Object NodeType2Object 

第2期

而不是:

private activeChannels 0; 

您需要的關鍵字variable

private variable activeChannels 0 
0

OK ..解決方法很簡單..加上美元符號$反對,他們是由裁判通過在proc對象 - 謝謝多納爾 即

方法getTotalUsedChannels {節點1節點2} {

組Node1Channels [$ Node1上getActiveChannels]

組Node2Channels [$節點2 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 
相關問題