pijush has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Thanks in advance
-Pijush
I am trying to learn a OO programming in perl. I have writtten following code to learn inheritance.
In the above code every methods present in Animal can be accessed by Horse or Sheep object. But if I want to add a private method to Animal class, how can I implement this? I tried to find out on Net and found out there are modules present public, private, but if I want to implement without using those modules, how can I implement this feature?{ package Animal; sub new { my $class = shift; my $name = shift; bless \$name, $class; } sub name { my $self = shift; ref $self ? $$self : "an unnamed $self"; } sub speak { my $class = shift; print "a $$class goes ", $class->sound, "!\n"; } sub eat { my $self = shift; my $food = shift; print $self->name, " eats $food.\n"; } } { package Horse; @ISA = qw (Animal); sub sound {"neigh"}; } { package Sheep; @ISA = qw (Animal); sub sound {"blahhhhhhhh"}; } my $horse1 = Horse->new("H1"); $horse1->speak; print "Horse sounds ", Horse->sound, "\n"; $horse1->eat("hay"); Sheep->eat("grass"); Sheep->speak;
Thanks in advance
-Pijush
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How can I add private data/methods in parent class?
by PodMaster (Abbot) on Jun 22, 2004 at 07:02 UTC | |
Re: How can I add private data/methods in parent class?
by dakkar (Hermit) on Jun 22, 2004 at 14:00 UTC | |
by pijush (Scribe) on Jun 23, 2004 at 10:31 UTC | |
Re: How can I add private data/methods in parent class?
by saintmike (Vicar) on Jun 22, 2004 at 05:56 UTC | |
by pijush (Scribe) on Jun 22, 2004 at 06:23 UTC | |
Re: How can I add private data/methods in parent class?
by ihb (Deacon) on Jun 22, 2004 at 13:54 UTC |
Back to
Seekers of Perl Wisdom