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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.