For 2023 - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
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 as below, or enter http://melksh.am/nnnn for individual articles Q906 - Object Orientation and General technical topics - Object Orientation: Individual Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25) [507] Introduction to Object Oriented Programming - (2005-11-27) [1543] Learning Object Oriented Principles (and perhaps Java) - (2008-02-17) [1864] Object Oriented Perl - First Steps - (2008-11-01) [1925] Introduction to Object Oriented Programming - (2008-12-06) [2171] Cleaning up redundant objects - (2009-05-11) [2173] Basic OO principles - (2009-05-11) [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26) [3436] Moving from scripting to Object Orientation in Python - (2011-09-13) [3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06) [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28) [4448] What is the difference between a function and a method? - (2015-03-04) [4591] From single block to structure and object oriented programming - (2015-12-02) [4650] Why populate object with values as you construct them? - (2016-02-18)
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!
|
4759 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, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).
|
|