Created
March 14, 2018 21:51
-
-
Save tmitzka/8d0ae3e814c371622ec7063193e7fb70 to your computer and use it in GitHub Desktop.
Getting to grips with classes and inheritance
This file contains 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 Cat(): | |
"""A prototype model for a cat.""" | |
def __init__(self, name, age, eye_color, weight_kg = 3): | |
"""Initialize attributes.""" | |
self.name = name | |
self.age = age | |
self.eye_color = eye_color | |
self.weight_kg = weight_kg | |
print(f"Welcome, {self.name}!") | |
def meow(self): | |
"""Simulate a cat meowing.""" | |
print(f"\n{self.name.title()} just meowed. What's wrong, buddy?") | |
def scratch(self): | |
"""Simulate a cat scratching someone.""" | |
print(f"\n{self.name.title()} just scratched me. Ow, stop it!") | |
class CyberCat(Cat): | |
"""A prototype model for a cybernetically enhanced cat.""" | |
def __init__(self, name, age, eye_color, weight_kg = 10): | |
"""Initialize attributes.""" | |
super().__init__(name, age, eye_color, weight_kg) | |
print("You look ... different.") | |
def shoot_lasers(self): | |
"""Simulate laser beams shooting from the cyber cat's eyes.""" | |
print("\nBZZZZZZZRT!!") | |
print(f"Deadly lasers shoot from the cat's {self.eye_color} eyes.") | |
def scratch(self): | |
"""Simulate a cyber cat scratching someone.""" | |
super().scratch() | |
print("My ... my arm is gone. I think I need a doctor.") | |
mr_jinx = CyberCat("Mr. Jinx", 6, "green") | |
mr_jinx.meow() | |
mr_jinx.shoot_lasers() | |
mr_jinx.scratch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment