-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop_task2.py
More file actions
75 lines (57 loc) · 2.24 KB
/
Copy pathoop_task2.py
File metadata and controls
75 lines (57 loc) · 2.24 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
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
72
73
74
75
"""Task 2: Inheritance and Polymorphism
In this task, you will create a simple class hierarchy
to demonstrate inheritance and polymorphism.
"""
import re
from oop_task1 import Person
class Student(Person):
"""The Student class inherits from the Person class
and adds additional attributes for phone and track.
"""
def __init__(self, name, country, date_of_birth, phone, track):
"""Initialize the Student object with name, country,
date of birth, phone number, and track."""
super().__init__(name, country, date_of_birth)
self.phone = phone
self.track = track
@property
def phone(self):
"""Return the phone number of the student."""
return self._phone
@phone.setter
def phone(self, phone):
"""Validate the phone number to ensure
it is 11 digits and starts with specific patterns."""
if re.match(r"^01(0|1|2|5)\d{8}$", phone):
self._phone = phone
else:
raise ValueError("Phone number must be 11 digits.")
def print_track(self):
"""Print the track of the student."""
print(f"Track : {self.track}")
def print_name(self):
"""Print the name of the student."""
print(f"Student Name : {self.name}")
class Teacher(Person):
"""The Teacher class inherits from the Person class and
adds additional attributes for salary and subject.
"""
def __init__(self, name, country, date_of_birth, subject):
"""Initialize the Teacher object with name, country,
date of birth, salary, and subject."""
super().__init__(name, country, date_of_birth)
self.subject = subject
def print_subject(self):
"""Print the subject taught by the teacher."""
print(f"Subject : {self.subject}")
def print_name(self):
"""Print the name of the teacher."""
print(f"Teacher Name : {self.name}")
def print_name(human):
"""Print the name of the human, whether it is a student or a teacher."""
human.print_name()
person1 = Person("Salem", "Egypt", "01-02-1998")
student1 = Student("Ahmed", "Egypt", "06-03-2002", "01005162237", "Python")
teacher1 = Teacher("Mohamed", "Egypt", "08-08-1949", "Mathematics")
print_name(student1)
print_name(teacher1)