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;
In reply to How can I add private data/methods in parent class? by pijush
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |