aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2016-03-24 20:50:23 +1000
committerSteve Bennett <steveb@workware.net.au>2016-03-27 11:01:48 +1000
commit45c9e90f3956ae5b8e561be046153bcb6ffc0985 (patch)
tree05994e1aed852670a235fd7bce0d22c5d81ef235 /examples
parentbd25a29a3766bcf9327c8c8d032d0bb773ca4e6a (diff)
downloadjimtcl-45c9e90f3956ae5b8e561be046153bcb6ffc0985.zip
jimtcl-45c9e90f3956ae5b8e561be046153bcb6ffc0985.tar.gz
jimtcl-45c9e90f3956ae5b8e561be046153bcb6ffc0985.tar.bz2
oo: constructor, unknown and bug fixes
- Added support for constructor, runs on new object creation - Added support for "unknown" method - Rename some dispatch variables (add double underscore) to avoid collision with user variables Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples')
-rw-r--r--examples/ootest.tcl17
1 files changed, 16 insertions, 1 deletions
diff --git a/examples/ootest.tcl b/examples/ootest.tcl
index d3d48c3..731e46a 100644
--- a/examples/ootest.tcl
+++ b/examples/ootest.tcl
@@ -13,6 +13,14 @@ puts "Account vars=[Account vars]"
puts "Account methods=[Account methods]"
puts ""
+# Create a constructor. This does validation, but it could
+# do other things
+Account method constructor {} {
+ if {$balance < 0} {
+ error "Can't initialise account with a -ve balance"
+ }
+}
+
# Now flesh out the class with some methods
# Could use 'Account method' here instead
Account method deposit {amount} {
@@ -57,10 +65,17 @@ $a describe
puts ""
# Now create a new subclass
+# Could change the initial balance here too
class CreditAccount Account {
limit -1000
- balance -20
}
+
+CreditAccount method constructor {} {
+ # Dummy constructor
+ # If desired, manually invoke the baseclass constructor
+ super constructor
+}
+
# Override the 'withdraw' method to allow overdrawing
CreditAccount method withdraw {amount} {
if {$balance - $amount < $limit} {error "Sorry $name, that would exceed your credit limit of [expr -$limit]"}