-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_oop_basics.py
More file actions
39 lines (30 loc) · 1.03 KB
/
09_oop_basics.py
File metadata and controls
39 lines (30 loc) · 1.03 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
38
39
#!/usr/bin/env python3
class Person:
"""Simple class for OOP demo"""
def __init__(self, name: str, age: int):
self.name = name
self.age = age
def introduce(self) -> None:
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
def have_birthday(self) -> None:
self.age += 1
print(f"Happy birthday {self.name}! Now {self.age} years old.")
def oop_basics():
"""07_oop_basics.py - Classes, objects, methods, inheritance"""
p = Person("Emma", 27)
p.introduce()
p.have_birthday()
# Inheritance demo
class Employee(Person):
def __init__(self, name: str, age: int, job: str):
super().__init__(name, age)
self.job = job
def introduce(self) -> None:
print(f"Hi, I'm {self.name}, a {self.job}, {self.age} years old.")
# Create an employee object and call the introduce method
emp = Employee("Bob", 35, "Software Engineer")
emp.introduce()
def main():
oop_basics()
if __name__ == '__main__':
main()