내 자신의 클래스를 tablelist
에서 상속받을 수 있습니까? 이 코드는 작동하지 않습니다Tcl : 테이블 목록에서 상속
package require tablelist
::itcl::class myTableList {
inherit ::tablelist
}
그리고 어떻게 생성자를 작성해야합니다 (명령 inherit
에서의 실패)?
P. tablelist
버전은 4.8
감사합니다.
내 자신의 클래스를 tablelist
에서 상속받을 수 있습니까? 이 코드는 작동하지 않습니다Tcl : 테이블 목록에서 상속
package require tablelist
::itcl::class myTableList {
inherit ::tablelist
}
그리고 어떻게 생성자를 작성해야합니다 (명령 inherit
에서의 실패)?
P. tablelist
버전은 4.8
감사합니다.
상속 가능한 클래스가 아니기 때문에 tablelist에서 상속받을 수는 없지만 수는이며 외부에 기능을 추가 할 수 있습니다. 가장 간단한 경우 - 메소드 수정 또는 추가 - 위임만으로 수행 할 수 있습니다. 방법은 다음과 같습니다 (TclOO 사용)입니다 :
### This metaclass makes doing the construction look like standard Tk.
oo::class create WidgetWrapper {
# It's a metaclass, so it inherits from oo::class
superclass oo::class
method unknown {methodName args} {
# See if we actually got a widget name; do the construction if so
if {[string match .* $methodName]} {
set widgetName $methodName
# There's a few steps which can't be done inside the constructor but have to
# be at the factory level.
set obj [my new $widgetName {*}$args]
rename $obj $widgetName
# Tk widget factories *MUST* return the path name as the name of the widget.
# It *MUST NOT* be colon-qualified, and it *MUST* refer to a widget.
return $widgetName
}
# Don't know what's going on; pass off to standard error generation
next $methodName {*}$args
}
unexport new create unknown
}
### This does the actual wrapping.
WidgetWrapper create WrappedTablelist {
constructor {pathName args} {
# Make the widget and *rename* it to a known name inside the object's
# private namespace. This is magical and works.
rename [tablelist::tablelist $pathName {*}$args] widget
}
# Delegate unknown method calls to the underlying widget; if they succeed,
# bake the delegation in more permanently as a forward.
method unknown {methodName args} {
try {
return [widget $methodName {*}$args]
} on ok {} {
oo::objdefine [self] forward $methodName widget $methodName
}
}
}
그런 다음 당신은 당신이 원하는 행동을 정의 /를
WrappedTablelist
에서 상속하고 추가 할 수 있습니다. 위임 초기화를 수행하기 위해
unknown
을 사용하는 것은 다소 지저분하지만, 테이블 목록에는 방대한 메소드가 있으므로 여기에 모두 나열하면 약간 고통 스러울 것입니다.
아마도 itcl과 비슷한 스키마를 사용할 수는 있지만 잘 모르겠습니다.
Tablelist는 [incr Tcl] (또는 Tcl의 다른 객체 시스템)을 사용하지 않으므로 상속받을 수 없습니다.