in reply to Understanding what Inheriting is.
Say you have some stock footage for an action-adventure
TV show, and you want to re-use it several times
without anyone noticing. This week, we are going to
flip a jeep and distract some guards.
This week's episode uses the ordnance module, which inherits from the stock_footage module.use strict; use ordnance; my $T= new ordnance(); $T->flip_jeep(); sleep 2; $T->firecracker();
Here is ordnance.pm:
and here is stock_footage.pm:package ordnance; use stock_footage; @ISA=qw(stock_footage); sub new { my $this = shift; my $class = ref($this) || $this; my $self = {}; bless $self, 'ordnance'; } sub flip_jeep { my $this = shift; my $class = ref($this) || $this; print "pull grenade pin\n"; sleep 2; $class->SUPER::flip_jeep(); } sub firecracker { my $this = shift; my $class = ref($this) || $this; print "light fuse\npsssss....\n"; sleep 2; print "Bang! Bang! Bang!\n"; $class->SUPER::distraction(); } 1;
So, someone can write this week's show using my ordnance routines, and they won't know that they are actually using stock_footage. Of course, I would write pod documentation for my ordnance routines so that the caller doesn't have to look at the ordnance code to know how to use it.package stock_footage; sub flip_jeep { print "And the jeep flies into the air and flips!\n"; } sub distraction { print "And the guards are momentarily distracted!\n"; }
I have not written much code that uses inheritance, but I'm sure that other monks can correct me if I have messed up my code. There are many things you can do with inheritance, just as there are many things you can do with a for loop. I would like an even more brief example, if someone can figure one out.
UPDATE:Fixed problems pointed out by tilly and petdance.
It should work perfectly the first time! - toma
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Understanding what Inheriting is.
by tilly (Archbishop) on Aug 13, 2001 at 15:59 UTC | |
|
Directions on military supplies
by petdance (Parson) on Aug 13, 2001 at 18:49 UTC | |
|
Re: Re: Understanding what Inheriting is.
by Sifmole (Chaplain) on Aug 13, 2001 at 16:45 UTC | |
by toma (Vicar) on Aug 14, 2001 at 06:08 UTC |