-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop.py
More file actions
115 lines (92 loc) · 3.53 KB
/
Copy pathoop.py
File metadata and controls
115 lines (92 loc) · 3.53 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# ===========================================
# OOP PLAYGROUND PROJECT
# Learn about: classes, objects, attributes, methods,
# constructors, encapsulation, inheritance, polymorphism
# Class definition: Blueprint for creating objects
# Objects: Instances of a class
# Attributes: Variables inside a class
# Methods: Functions inside a class
# Constructor (__init__): Initializes object attributes
# Encapsulation: Private attributes with getters/setters
# Inheritance: Child class extending a parent
# Polymorphism: Methods behaving differently in subclasses
# ===========================================
# ---------------------------
# CLASS & OBJECTS
# ---------------------------
class Student:
"""A class to represent a student."""
# Constructor: initializes an object with attributes
def __init__(self, name, age, grade):
self.name = name # public attribute
self.age = age
self.__grade = grade # private attribute (encapsulation with __)
# Method: a function that belongs to the class
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
# Getter method to access private attribute
def get_grade(self):
return self.__grade
# Setter method to safely change private attribute
def set_grade(self, grade):
if 0 <= grade <= 100: # simple validation
self.__grade = grade
else:
print("Invalid grade. Must be between 0 and 100.")
# ---------------------------
# INHERITANCE
# ---------------------------
class GraduateStudent(Student):
"""A class that inherits from Student"""
def __init__(self, name, age, grade, thesis_title):
# Call parent constructor using super()
super().__init__(name, age, grade)
self.thesis_title = thesis_title
# Polymorphism: override introduce() method
def introduce(self):
print(f"I am {self.name}, a graduate student.")
print(f"My thesis is titled: '{self.thesis_title}'")
# ---------------------------
# OBJECT CREATION
# ---------------------------
# Create an object (instance) of Student
student1 = Student("Alice", 20, 88)
student1.introduce()
print("Alice's grade:", student1.get_grade())
# Try changing grade using setter
student1.set_grade(95)
print("Alice's new grade:", student1.get_grade())
# Create another object
student2 = Student("Bob", 19, 76)
student2.introduce()
# Create a GraduateStudent object
grad_student = GraduateStudent("Charlie", 25, 90, "AI in Healthcare")
grad_student.introduce()
print("Charlie's grade:", grad_student.get_grade())
# ---------------------------
# USING A LIST OF OBJECTS
# ---------------------------
# Store objects in a list and loop through them
print("\n--- List of Students ---")
students = [student1, student2, grad_student]
for s in students:
s.introduce() # polymorphism in action (different introduce() behavior)
# Concepts Illustrated:
#
# Class: Defines a blueprint using 'class Student'.
#
# Object: Creates an instance with 'student1 = Student("Alice", 20, 88)'.
#
# Attributes: Includes 'self.name', 'self.age', and 'self.__grade'.
#
# Methods: Functions like 'introduce()', 'get_grade()', and 'set_grade()'.
#
# Constructor: '__init__' method initializes attributes.
#
# Encapsulation: '__grade' is private, accessed via getter & setter.
#
# Inheritance: 'GraduateStudent' inherits from 'Student'.
#
# Polymorphism: 'introduce()' method behaves differently in 'Student' and 'GraduateStudent'.
#
# List of objects: Stores multiple students and iterates over them.