diff options
Diffstat (limited to 'python/unrelated/obj.py')
| -rw-r--r-- | python/unrelated/obj.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/python/unrelated/obj.py b/python/unrelated/obj.py new file mode 100644 index 0000000..c20502f --- /dev/null +++ b/python/unrelated/obj.py @@ -0,0 +1,71 @@ +# Animal +# base class +class Animal: + + def __init__(self, name, age): + self.name = name + self.age = age + + def preview(self): + print(f"{self.name} is a {self.creature} of {self.age} years old") + + + +# Dog +# extends Animal +class Dog(Animal): + + voice = 'woof! ~@_@~' + + def __init__(self, name, age): + self.name = name + self.age = age + self.creature = 'dog' + + def speak(self): + print(self.voice) + + + +# Cat +# extends Animal +class Cat(Animal): + + voice = 'meao!! >^_^<' + + def __init__(self, name, age): + self.name = name + self.age = age + self.creature = 'cat' + + def speak(self): + print(self.voice) + + +# functions +# --- -- -- - - - + +def newLine(): + print(" ") + +def attrs(obj): + for attribute, value in obj.__dict__.items(): + print(f"{attribute}: {value}") + + +# instances +# --- -- -- - - - + +d = Dog('Ersi', 8) +r = Cat('Riri', 10) + +d.preview() +d.speak() +attrs(d) + +newLine() + +r.preview() +r.speak() +attrs(r) + |
