Keep It Simple, Stupid | |
PerlMonks |
Re: Base class constructor that blesses into subclassby Basilides (Friar) |
on Jul 06, 2002 at 22:45 UTC ( [id://179896]=note: print w/replies, xml ) | Need Help?? |
I had a similar thing recently, and funnily enough, in a similar (linguistic) context. One problem comes to my mind: if you return child objects from your parent constructor, the subclasses can have their own methods, but it's not convenient for them to have any object variables, in addition to those they inherit.
Eg, an abstract Word class may have variables english and greek, but its child class, Noun, may need nom_singular, whereas child class, Verb, may need to be constructed with infinitive. Perhaps at the moment you can't imagine, say, your Interrogative and Declarative classes diverging like that, but for the sake of expansion, I'd be cautious of blessing into a subclass. I forgot to say, an alternative might be to test the string to decide which kind of subclass you need, then create an instance of that subclass by calling its own constructor, but in each of the constructors for the subclasses, have a call to the parent's constructor (like super in Java). This is what I did, and the PerlMonks very kindly showed my how to do it.
In Section
Seekers of Perl Wisdom
|
|