Untitled
unknown
plain_text
2 months ago
3.5 kB
8
Indexable
class Persoana:
def __init__(self, nume, email, cnp):
# PUBLIC - poate fi accesat direct din exterior
self.nume = nume
# PROTECTED - prin conventie, nu ar trebui accesat direct din exterior
self._email = email
# PRIVATE - Python modifica intern numele variabilei
self.__cnp = cnp
# METODA PUBLICA
def afiseaza_informatii(self):
print(f"Nume: {self.nume}")
print(f"Email: {self._email}")
print(f"CNP: {self.__cnp}")
# METODA PROTECTED
def _verifica_email(self):
return "@" in self._email
# METODA PRIVATE
def __mascheaza_cnp(self):
return self.__cnp[:3] + "********"
# Metoda publica prin care putem folosi o metoda private
def afiseaza_cnp_mascat(self):
print(f"CNP mascat: {self.__mascheaza_cnp()}")
class Student(Persoana):
def __init__(self, nume, email, cnp, grupa, medie):
super().__init__(nume, email, cnp)
# PUBLIC
self.grupa = grupa
# PROTECTED
self._medie = medie
# PRIVATE
self.__numar_matricol = "STU12345"
def afiseaza_student(self):
print("=== Student ===")
print(f"Nume: {self.nume}") # acces public
print(f"Email: {self._email}") # acces protected, permis in subclasa
print(f"Grupa: {self.grupa}")
print(f"Medie: {self._medie}")
# Nu putem accesa direct self.__cnp, deoarece este private in clasa Persoana
# print(self.__cnp) # ar produce eroare
def verifica_date_student(self):
if self._verifica_email():
print("Emailul studentului este valid.")
else:
print("Emailul studentului este invalid.")
def afiseaza_numar_matricol(self):
print(f"Numar matricol: {self.__numar_matricol}")
class Profesor(Persoana):
def __init__(self, nume, email, cnp, disciplina):
super().__init__(nume, email, cnp)
# PUBLIC
self.disciplina = disciplina
def afiseaza_profesor(self):
print("=== Profesor ===")
print(f"Nume: {self.nume}")
print(f"Email: {self._email}")
print(f"Disciplina: {self.disciplina}")
class Curs:
def __init__(self, titlu, profesor):
# PUBLIC
self.titlu = titlu
# PROTECTED
self._profesor = profesor
# PRIVATE
self.__studenti = []
def adauga_student(self, student):
self.__studenti.append(student)
def afiseaza_curs(self):
print("=== Curs ===")
print(f"Titlu curs: {self.titlu}")
print(f"Profesor: {self._profesor.nume}")
print("Studenti inscrisi:")
for student in self.__studenti:
print(f" {student.nume}, grupa {student.grupa}")
# Program principal
student1 = Student(
nume="Ana Popescu",
email="[email protected]",
cnp="5050101123456",
grupa="AVI-1",
medie=9.45
)
profesor1 = Profesor(
nume="Dr. Mihai Ionescu",
email="[email protected]",
cnp="1800202123456",
disciplina="Programare Orientata pe Obiect"
)
curs1 = Curs(
titlu="Programare in Python",
profesor=profesor1
)
curs1.adauga_student(student1)
# Accesarea atributelor publice
print(student1.nume)
print(student1.grupa)
print(profesor1.disciplina)
print()
# Accesarea atributelor protected
# Este posibila tehnic, dar nu este recomandata din exteriorul clasei
print(student1._email)
print(student1._medie)
print()
Editor is loading...
Leave a Comment