-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_inheritance2.PY
More file actions
37 lines (27 loc) · 1.01 KB
/
class_inheritance2.PY
File metadata and controls
37 lines (27 loc) · 1.01 KB
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
class Animal:
def __init__(self, nickname: str, age: int):
self.nickname = nickname
self.age = age
def make_sound(self):
pass
class Cat(Animal):
def make_sound(self) -> str:
return "Meow"
class Dog(Animal):
def __init__(self, nickname: str, age: int, breed: str):
super().__init__(nickname, age) # Викликаємо конструктор базового класу
self.breed = breed # Додаємо нову властивість
def make_sound(self) -> str:
return "Woof"
def chase_tail(self) -> str:
return f"{self.nickname} is chasing its tail!"
class Cow(Animal):
def make_sound(self):
return "Moo"
my_cat = Cat("Simon", 4)
my_cow = Cow("Bessie", 3)
print(my_cat.make_sound()) # Виведе "Meow"
print(my_cow.make_sound()) # Виведе "Moo"
my_dog = Dog("Rex", 5, "Golden Retriever")
print(my_dog.make_sound()) # Виведе "Woof"
print(my_dog.chase_tail()) # Виведе "Rex is chasing its tail!"