?? class_example.tcl
字號:
Class Bagel ;# 定義類Bagel
Bagel instproc init {args} { ;# Bagel類的初使化函數
$self set toasted 0
eval $self next $args
}
Bagel instproc toast {} { ;# 定義Bagel類的成員函數toast
$self instvar toasted
incr toasted
if {$toasted>1} then {
error "something's burning!"
}
return {}
}
Class SpreadableBagel -superclass Bagel ;# SpreadableBagel繼承于類Bagel
SpreadableBagel sBagel ;# 生成SpreadableBagel類的對象sBagel
SpreadableBagel instproc init {args} { ;# SpreadableBagel的初使化函數
$self set toppings {}
eval $self next $args ;# 調用父類的初使化函數
}
sBagel set toasted ;# 父類Bagel的變量toasted
sBagel toast ;# 實際調用父類Bagel的toast函數
sBagel toast ;# 實際調用父類Bagel的toast函數
SpreadableBagel info superclass ;# 查看父類信息
SpreadableBagel info heritage ;# 查看繼承樹信息
Bagel instproc taste {} { ;# Bagel類增加taste函數
$self instvar toasted
if {$toasted == 0} then {
return raw!
} elseif {$toasted == 1} then {
return toasty
} else {
return burnt!
}
}
SpreadableBagel instproc spread {args} { ;# SpreadableBagel類增加spead函數
$self instvar toppings
set toppings [concat $toppings $args]
return $toppings
}
SpreadableBagel instproc taste {} { ;# SpreadableBagel類增加taste函數
$self instvar toppings
set t [$self next] ;# $self next 調用父類Bagel的taste函數
foreach i $toppings {
lappend t $i ;# 在變量$t后增加$i的值
}
return $t
}
Class Sesame ;# 定義類Sesame
Sesame instproc taste {} {
concat [$self next] "sesame"
}
Class Onion
Onion instproc taste {} {
concat [$self next] "onion"
}
Class SesameOnionBagel -superclass {Sesame Onion SpreadableBagel}
SesameOnionBagel info heritage ;# 查看SesameOnionBagel類的繼承樹信息
SesameOnionBagel abagel -spread butter ;# 調用spead函數,來自SpreadableBagel類
abagel taste
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -