|
A first demonstration of OO, including polymorphism
"Object Oriented" often means big and heavy code even for the first example application demos ... since OO works really well when you're meeting requirements beyond those which are small enough to be described as 'trivial'. So I'm very pleased with this little demonstration in Python which shows - all in one file - the definition of four different classes, the building of a list of objects of various different sorts, and then a loop that goes through all the objects using 'polymorphism' to ensure that the right accessor code is run on the right piece of data.
Scenario - monthly bills need to be multiplied by 12 for the annual cost, Council tax bills come in 10 instalments, and annual payers get a 10% discount.
class monthly:
def __init__(self,xxx):
self.val = xxx
def yearly(self):
return self.val * 12
class annual:
def __init__(self,xxx):
self.val = xxx
def yearly(self):
return self.val * 0.9
class council:
def __init__(self,xxx):
self.val = xxx
def yearly(self):
return self.val * 10
# ----------------------------
david = monthly(25)
john = monthly(14)
gerry = annual(125)
lisa = council(112)
debtors = (david, john, gerry, lisa)
for debtor in debtors:
print debtor.yearly()
Please note - this code is BELOW my minimum standards for a live application. I would expect to see comments, I would expect better variable naming, and I would use inheritance to save the need for a duplicated constructor.
Sample output:
92: grahamellis$ python accts
300
168
112.5
1120
92: grahamellis$ (written 2009-09-04, updated 2009-09-05)
Associated topics are indexed under Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28) [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06) [3436] Moving from scripting to Object Orientation in Python - (2011-09-13) [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26) [2173] Basic OO principles - (2009-05-11) [2171] Cleaning up redundant objects - (2009-05-11) [1925] Introduction to Object Oriented Programming - (2008-12-06) [1864] Object Oriented Perl - First Steps - (2008-11-01) [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17) [507] Introduction to Object Oriented Programming - (2005-11-27) [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
47d5
Some other Articles
Signwriting is dead. Long live the sign.Easing off in our 50s?From Lymington by train - last of the slammersTwo days of demonstration scripts in PythonA first demonstration of OO, including polymorphismLymington, New Forest - some picturesGreat Western Route Utilisation Strategy - Draft for ConsultationDynamic / changing images on your web pageWriting with our customers words`Of Course` is back!
|
4088 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|