Friday, May 3, 2019

Inheritance Example

  Praveen Tiwari       Friday, May 3, 2019

Introduction

Clock Calendar Class Inheritance
There aren't many good examples on inheritance available on the web. They are either extremely simple and artificial or they are way too complicated. We want to close the gap by providing an example which is on the one hand more realistic - but still not realistic - and on the other hand simple enough to see and understand the basic aspects of inheritance. In our previous chapter, we introduced inheritance formally. 

To this purpose we define two base classes: One is an implementation of a clock and the other one of a calendar. Based on these two classes, we define a class CalendarClock, which inherits both from the class Calendar and from the class Clock.

The Clock Class

class Clock(object):
def __init__(self,hours=0, minutes=0, seconds=0):
self.__hours = hours
self.__seconds = seconds
self.__minutes = minutes
self.__hours = hours
def set(self,hours, minutes, seconds=0): self.__minutes = minutes
""" Time will be advanced by one second """
self.__seconds = seconds def tick(self): if self.__seconds == 59:
self.__hours = 0 if self.__hours==23 else self.__hours + 1
self.__seconds = 0 if (self.__minutes == 59): self.__minutes = 0
def display(self):
else: self.__minutes += 1; else: self.__seconds += 1;
return "%2d:%2d:%2d" % (self.__hours, self.__minutes, self.__seconds)
print("%d:%d:%d" % (self.__hours, self.__minutes, self.__seconds)) def __str__(self): x = Clock() print(x) for i in range(10000): x.tick()
print(x)




The Calendar Class

class Calendar(object):
months = (31,28,31,30,31,30,31,31,30,31,30,31)
self.__day = day
def __init__(self, day=1, month=1, year=1900):
self.__year = year
self.__month = month def leapyear(self,y): if y % 4:
return 1;
# not a leap year return 0; else: if y % 100:
return 1;
else: if y % 400: return 0 else:
self.__year = year
def set(self, day, month, year): self.__day = day self.__month = month def get():
max_days = months[self.__month-1]
return (self, self.__day, self.__month, self.__year) def advance(self): months = Calendar.months if self.__month == 2:
self.__month = 1
max_days += self.leapyear(self.__year) if self.__day == max_days: self.__day = 1 if (self.__month == 12): self.__year += 1
if __name__ == "__main__":
else: self.__month += 1 else: self.__day += 1 def __str__(self): return str(self.__day)+"/"+ str(self.__month)+ "/"+ str(self.__year)
print(x)
x = Calendar() print(x)
x.advance()




The Calendar-Clock Class

from clock import Clock
from calendar import Calendar
class CalendarClock(Clock, Calendar):
def __init__(self, day,month,year,hours=0, minutes=0,seconds=0):
Calendar.__init__(self, day, month, year)
def __str__(self):
Clock.__init__(self, hours, minutes, seconds)
x = CalendarClock(24,12,57)
return Calendar.__str__(self) + ", " + Clock.__str__(self) if __name__ == "__main__": print(x)
print(x)
for i in range(1000): x.tick() for i in range(1000):
x.advance()

logoblog

Thanks for reading Inheritance Example

Previous
« Prev Post

No comments:

Post a Comment