Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to sub class-es

by Fletch (Chancellor)
on May 15, 2020 at 03:44 UTC ( #11116798=note: print w/replies, xml ) Need Help??


in reply to How to sub class-es

Your question's a bit vague so I may be off chasing untamed ornithoids here; you might provide some more concrete, actual code and get a more relevant answer. That caveat aside . . .

It may be a side effect of your example wording but that almost sounds more like you want to model a has-a relationship rather than is-a. A Robo has an Arm, Feet, and a Head (of whatever cardinalities); but the whole in and of itself isn't a more specialized type of Arm. You'd then set up some sort of delegation on the Robo instance so that when you call aa Arm method it calls that method on the Arm instance belonging to it instead. If you're using Moose or the like there's going to be a handles declaration which you can say that calls to some set of methods should be forwarded to be called on the value in that slot. See Moose::Manual::Delegation for more details on wiring that up.

You then don't need to worry about chaining superclass constructors and what not, you just create your part instances and pass those components to the containing class' constructor.

Edit: Tweaked wording a bit. This SO thread (specifically the first answer's examples) may make more clear what I'm saying WRT inheritance versus composition.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: How to sub class-es
by exilepanda (Pilgrim) on May 15, 2020 at 11:42 UTC
    Thank you so much for remind me that it is not an is-a condition, but has-a. Totally agree with that. Perhaps I should ask "how to do has-a correctly" =) And I've added the code into my post.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11116798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2020-11-30 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?