Liste des Groupes | Revenir à cl tcl |
On Tue, 25 Jun 2024 22:29:46 -0000 (UTC), Rich wrote:
If you only *ever* have one dog, either are essentially the same.
>
But, if at some point you want to manage spot, frank, sam, donut, and
alexa, all of which are different dogs, then you can do:
>
snit::type dog {
method {tail wag} {} {return "Wag, wag"}
method {tail droop} {} {return "Droop, droop"}
}
>
Once to create a generic "dog" framework.
>
And then later do, all in the same single program instance:
>
dog spot
dog frank
dog sam
dog donut
dog alexa
>
And have five, fully independent dogs (each with their own independent
"data") that you can manipulate, without having to do anything special
at the time you create them.
>
I.e., you can wag spot's tail, give food to sam, and label alexa with
"last vet visit was 2024-06-21" (assuming you had mentods for 'feed'
and for 'last-vet-visit' in the generic framework.
And how is that better than this:
proc dog {dogname args} {
if {$args == "tail wag"} {return "$dogname Wag, wag"}
if {$args == "tail droop"} {return "$dogname Droop, droop"}
if {$args == "eats food"} {return "$dogname Nom, nom"}
if {$args == "goes to vet"} {return "$dogname Tries to run"}
}
puts [dog fido tail wag]
puts [dog lucky tail droop]
puts [dog charlie eats food]
puts [dog mitts goes to vet]
That will handle multiple dogs pretty well.
One "method," one "if." It's pretty much it, isn't it?
Les messages affichés proviennent d'usenet.