# - Update X for this instance - Note: I'd typically use a Getter / Setter for all variables to prevent incorrect data-types or usage # - We can set the name here, or just leave it in the class - Note: The same can be done in Rect but I omitted it to keep this example short. # - Run the BaseClass - Although Shape doesn't do anything, it's here to fall all the way through just in case. # - Object Creation - We'll use this to create a square: Square( 10 ) for example. # - Square extends Shape - A Square has the same width / height so we only need 1 value, we'll use X but it can be anything we want. Return "I am a " + self.name + " defined as " + type( self )._name_ + "(. # - Note: You need to use str( x ) to convert that object to a string - most data-types have an appropriate To-String function to do this and if not you'll be left with something like # - The ToString function for this class - it is what is called when you print( str( shape ) ) # - Shape Base-Class - This only holds the name of the object and nothing else. # - Basic example of classes and inheritance by using an easy to follow idea - shapes - and extending them. I added a few examples based on the other example in this thread ( area function and tostring method ) #. It is a disgusting habit which increases file size incredibly fast. Here's another example of inheritance - shapes.Įdit: I hate how this site converts python code to use spaces instead of tabs. There's no need to call super with arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |