Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
However, I have reason to keep the Base object inside {Parent}, as there are other constructors which will be invoked by other methods in Lite class.

There's a better way to do that by making the constructor in the parent class subclass-friendly. This involves checking the first argument you get in an OO constructor routine and identifying if it has a valid ref() associated with it. If you write your constructors as my example code below shows, you'll be making life easy for anyone else who wants to built on your code base.

In the code below, note a couple of things:

  1. Note that the $ex2 object in consumer.pl is created by using the constructor method of the $ex1 object, which is of the "Improved" subclass.
  2. See how the Parent class uses the ref() call to correctly determine when a subclass is re-using the Parent's constructor method.

Here's the example code structure:

File: consumer.pl

use strict; use warnings; use lib '.'; require Ex::Improved; my $ex1 = Ex::Improved->new(); # Show that the parent's constructor still works: my $ex2 = $ex1->new(); $ex1->setColor('green'); $ex1->setFood('bean'); $ex2->setColor('red'); $ex2->setFood('radish'); my $count = 0; # count each object we display for my $obj ($ex1, $ex2) { printf "Object: %d\n", ++$count; printf "Color: %s\n", $obj->getColor(); printf "Food: %s\n", $obj->getFood(); }

File: Ex/Parent.pm (the parent class)

package Ex::Parent; use strict; use warnings; sub new { my $class = shift; # This line lets subclass-created objects work: $class = ref($class) || $class; return bless { }, $class; } sub getColor { my $self = shift; return $self->{color} // undef; } sub setColor { my $self = shift; $self->{color} = shift or return 0; return 1; } 1;

File: Ex/Improved.pm (the subclass)

package Ex::Improved; use strict; use warnings; use parent 'Ex::Parent'; # This "improved" class lets callers get/set Food too! sub getFood { my $self = shift; return $self->{food} // undef; } sub setFood { my $self = shift; $self->{food} = shift or return 0; return 1; } 1;

In reply to Re^3: Inheritance confused by Apero
in thread Inheritance confused by exilepanda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found