Last active
March 13, 2020 10:53
-
-
Save dfbarrero/cd2e32f810dbd6f3f62ac7f33e2c67da to your computer and use it in GitHub Desktop.
Example of inheritance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal: | |
| def __init__(self): | |
| self.name = "Unknown" | |
| self.age = 10 | |
| def describe(self): | |
| print("Name: ", self.name) | |
| print("Age: ", self.age) | |
| class Dog(Animal): | |
| def bit(self): | |
| print(self.name + " has bitten") | |
| class Cat(Animal): | |
| def scratch(self): | |
| print(self.name + " has scratched") | |
| if __name__ == '__main__': | |
| snoopy = Dog() | |
| garfield = Cat() | |
| snoopy.name = "Snoopy" | |
| garfield.name = "Garfield" | |
| snoopy.bit() | |
| garfield.scratch() | |
| garfield.bit() # Error! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment