A
Anthony Roy
Hi all,
I am trying to solve a problem to do with inheritance which I just
cannot fathom. Four example files are at the bottom of this email which
I think are a pretty much minimal example of the problem.
I'm basically trying to inherit some methods from a super class. Parent
defines two methods, new and getClass. The file Inheritence.pl basically
creates an instance of each of two subclasses of Parent, printing out
the value returned by the getClass() method after each is created, and
then prints out the value returned by getClass on the first object
again. These are the results:
A: ChildA
B: ChildB
A: ChildB
even though I haven't explicitly changed any values in A. Surely the
first and third lines should be identical? What am I missing here, or am
I just better off avoiding inheritance in perl?
Note I am using ActiveState perl v5.8.1 on a Windows XP box.
Thanks,
Anthony Roy.
-------------------------------------------------------------
#Parent.pm
package Parent;
sub new {
my $class = shift;
my $self = {};
$self{Class} = $class;
bless $self, $class;
}
sub getClass{
my $self = shift;
$self{Class};
}
1;
-------------------------------------------------------------
#ChildA.pm
package ChildA;
use Parent;
@ISA = (Parent);
1;
-------------------------------------------------------------
#ChildB.pm
package ChildB;
use Parent;
@ISA = (Parent);
1;
-------------------------------------------------------------
#Inheritence.pl
use ChildA;
use ChildB;
my $childA = ChildA->new();
print "A: " . $childA->getClass() . "\n";
my $childB = ChildB->new();
print "B: " . $childB->getClass() . "\n";
print "A: " . $childA->getClass() . "\n";
I am trying to solve a problem to do with inheritance which I just
cannot fathom. Four example files are at the bottom of this email which
I think are a pretty much minimal example of the problem.
I'm basically trying to inherit some methods from a super class. Parent
defines two methods, new and getClass. The file Inheritence.pl basically
creates an instance of each of two subclasses of Parent, printing out
the value returned by the getClass() method after each is created, and
then prints out the value returned by getClass on the first object
again. These are the results:
A: ChildA
B: ChildB
A: ChildB
even though I haven't explicitly changed any values in A. Surely the
first and third lines should be identical? What am I missing here, or am
I just better off avoiding inheritance in perl?
Note I am using ActiveState perl v5.8.1 on a Windows XP box.
Thanks,
Anthony Roy.
-------------------------------------------------------------
#Parent.pm
package Parent;
sub new {
my $class = shift;
my $self = {};
$self{Class} = $class;
bless $self, $class;
}
sub getClass{
my $self = shift;
$self{Class};
}
1;
-------------------------------------------------------------
#ChildA.pm
package ChildA;
use Parent;
@ISA = (Parent);
1;
-------------------------------------------------------------
#ChildB.pm
package ChildB;
use Parent;
@ISA = (Parent);
1;
-------------------------------------------------------------
#Inheritence.pl
use ChildA;
use ChildB;
my $childA = ChildA->new();
print "A: " . $childA->getClass() . "\n";
my $childB = ChildB->new();
print "B: " . $childB->getClass() . "\n";
print "A: " . $childA->getClass() . "\n";