我剛剛開始使用itcl,並且找到了How to pass itcl object from one class to another?但這並沒有幫助我。itcl如何將對象傳遞給其他對象
我有以下對象定義:
package require Itcl
namespace eval FEM {
itcl::class Node {
variable nid
variable x
variable y
variable z
constructor {{NID 0} {coordX 0} {coordY 0} {coordZ 0}} {
set nid $NID
set x $coordX
set y $coordY
set z $coordZ
return $this}
method is {nid coordX coordY coordZ}
method printString
}
itcl::body Node::is {NID coordX coordY coordZ} {
set nid $NID
set x $coordX
set y $coordY
set z $coordZ}
itcl::body Node::printString {} {
return "$nid $x $y $z"}
# #######################################################
itcl::class CQUAD {
variable eid
variable pid
variable node1
variable node2
variable node3
variable node4
constructor {{EID 0} {PID 0} {NODE1 0} {NODE2 0} {NODE3 0} {NODE4 0}} {
set eid $EID
set pid $PID
set node1 $NODE1
set node2 $NODE2
set node3 $NODE3
set node4 $NODE4
return $this}
method is {EID PID NODE1 NODE2 NODE3 NODE4}
method printString
}
itcl::body CQUAD::is { EID PID NODE1 NODE2 NODE3 NODE4 } {
set eid $EID
set pid $PID
set node1 $NODE1
set node2 $NODE2
set node3 $NODE3
set node4 $NODE4}
itcl::body CQUAD::printString {} {
return "$eid $pid $node1 $node2 $node3 $node4"}
}
}
# ########################################################
# Test
for {set n 1} {$n <= 4} {incr n} {
set coord1 [expr {$n+1} ]
set coord2 [expr {$n+2} ]
set coord3 [expr {$n+3} ]
set NID N$n
::FEM::Node $NID $n $coord1 $coord2 $coord3
puts "[$NID printString]"
}
set PID 99
set EID 88
以下是錯誤的,因爲$N1
根本N1
,而不是一個「節點」 N1
::FEM::CQUAD MyQuad $EID $PID $N1 $N2 $N3 $N4
但是我怎麼傳遞類型的對象節點到CQUAD類型的對象? 我也試過:
::FEM::CQUAD MyQuad $EID $PID $::FEM:Node::N1 $::FEM:Node::N2 $::FEM:Node::N3 $::FEM:Node::N4
另外,如何刪除一個對象? unset $N1
是錯誤unset $::FEM::Node::N1
也是假
最後一個問題:假設我有與它Node類型的對象CQUAD對象: 我怎麼能獲得例如節點2的y座標CQUAD 1的?
使用N1我得到的命名空間::: N1所以itcl :: delete節點$ :: N1應該刪除它,但我得到:糟糕的選項節點,應該是....只是對我來說:N1是對象名稱,「節點」是對象類型? – Lumpi
要刪除,'object'是一個文字字符串。所以你的情況是:itcl :: delete object :: N1 –