blob: c20502f11e1c1169d8de0a136aea7be4ce98341a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
|