55 lines
1.3 KiB
Plaintext
55 lines
1.3 KiB
Plaintext
abstract class edible
|
|
string name
|
|
procedure eat(); -- (virtual)
|
|
end class
|
|
|
|
class foodbox2
|
|
sequence contents = {}
|
|
procedure add(edible food)
|
|
if food.eat=NULL then -- (optional)
|
|
throw("eat() not implemented")
|
|
end if
|
|
this.contents = append(this.contents,food)
|
|
end procedure
|
|
procedure dine()
|
|
integer l = length(this.contents)
|
|
string s = iff(l=1?"":"s")
|
|
printf(1,"foodbox2 contains %d item%s\n",{l,s})
|
|
for i=1 to l do
|
|
-- this.contents[i].eat() -- not supported, sorry!
|
|
-- compiler needs some more type hints, such as:
|
|
edible food = this.contents[i]
|
|
food.eat()
|
|
end for
|
|
end procedure
|
|
end class
|
|
foodbox2 lunchbox2 = new()
|
|
|
|
class fruit2 extends edible
|
|
procedure eat()
|
|
printf(1,"mmm... %s\n",{this.name})
|
|
end procedure
|
|
end class
|
|
fruit2 banana2 = new({"banana"})
|
|
|
|
class clay2
|
|
string name = "common fletton"
|
|
end class
|
|
clay2 brick2 = new()
|
|
|
|
class drink extends edible
|
|
procedure eat()
|
|
printf(1,"slurp... %s\n",{this.name})
|
|
end procedure
|
|
end class
|
|
drink milkshake = new({"milkshake"})
|
|
|
|
lunchbox2.add(banana2)
|
|
try
|
|
lunchbox2.add(brick2) -- triggers typecheck
|
|
catch e
|
|
printf(1,"%s line %d: %s\n",{e[E_FILE],e[E_LINE],e[E_USER]})
|
|
end try
|
|
lunchbox2.add(milkshake)
|
|
lunchbox2.dine()
|