![]() ![]() _year ) if _name_ = "_main_" : x = Calendar () print ( x ) x. The key to understanding Inheritance is that it provides code re-usability. We can represent this relationship in code with the help of inheritance. That means they’ve inherited the properties of class vehicles, i.e., all are used for transportation. _day += 1 def _str_ ( self ): return str ( self. Here’s an example car, bus, bike all of these come under a broader category called Vehicle. Inheritance allows us to reuse code from a class. For example: a car is a vehicle, or a dog is an animal. Inheritance is usually referred to as an Is-A relationship. A software modelling approach of OOP enables extending the capability of an existing class to build a new class, instead of building from scratch. _year ) def advance ( self ): months = Calendar. Inheritance allows us to define a sub or child class which inherits functionality from another class, otherwise known as a base class or a parent class. In addition, I have a second base class CharClass (object), with two derived classes: Wizard (CharClass) and Warrior (CharClass). Then I created two derived classes from this, Elf (CharRace) and Dwarf (CharRace). I created a base class CharRace (object). _year = year def get (): return ( self, self. Im just trying to put together a simple RPG example in order to learn OOP in Python. Lets see an example of a parent class FarmAnimal, a child class Duck to show how inheritance works: class FarmAnimal: def init(self, typeAnimal. _year = year def leapyear ( self, y ): if y % 4 : # not a leap year return 0 else : if y % 100 : return 1 else : if y % 400 : return 0 else : return 1 def set ( self, day, month, year ): self. ![]()
0 Comments
Leave a Reply. |