Posted to tcl by apw at Mon Sep 10 13:21:00 GMT 2007view raw

  1. package require Itcl 4.0
  2.  
  3. ::itcl::class Person {
  4. private variable nbPersons 0
  5. protected variable name
  6. protected variable tool
  7. constructor {{myname "Steve McQueen"}} {
  8. set name $myname
  9. incr nbPersons
  10. puts "Person named : '$name' created"
  11. }
  12. destructor {
  13. incr nbPersons -1
  14. puts "Person named : '$name' deleted"
  15. }
  16.  
  17. }
  18.  
  19. ::itcl::class Tool {
  20. method tell {message} {
  21. return "Tool is telling you : '$message'"
  22. }
  23. method sing {who what} {
  24. return "$who sings : '$what'"
  25. }
  26. }
  27.  
  28. set t [Person #auto "Steve McQueen"]
  29. ::itcl::mixin add ::Person ::Tool
  30. ::itcl::forward add $t singing $t sing "Queen"
  31. puts [$t tell "This is the truth : I am a liar"]
  32. puts [$t singing "We will rock you!"]
  33. :::itcl::delete object $t
  34.  
  35. Result in both versions:
  36.  
  37. Person named : 'Steve McQueen' created
  38. Tool is telling you : 'This is the truth : I am a liar'
  39. Queen sings : 'We will rock you!'
  40. Person named : 'Steve McQueen' deleted
  41.