Created
April 6, 2025 11:33
-
-
Save thinkphp/e37cc63cdb08ea9cec1e4b430c491a5e to your computer and use it in GitHub Desktop.
agg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main( a, b = None, c = None): | |
print( a, b, c ) | |
main( 1, 2, 3 ) | |
# Agregare in programarea orientata obiect | |
""" | |
Agregarea este un concept important in programarea orientata pe obiecte si reprezinta o relatie de tip "has a" (are un) intre doua clase. | |
In aceasta relatie de tip "has a" o clasa contine o referinta catre un obiect al altei clase. dar acel obiect poate exista independent de clasa care il contine. | |
In agregare: | |
- o clasa container contine o referinta catre un obiect al alte clase | |
- obiectul inclus poate exista independent de obiectul container | |
- relatia este de tipul "has-a" | |
- este o forma de asociere mai slaba decat compozitia. | |
Exemplu elocvent de agregare | |
has-a | |
Department -------> Professor | |
""" | |
class Professor: | |
# constructorul clasei: name, specializare | |
def __init__(self, name, specializare): | |
self.name = name; | |
self.specializare = specializare | |
def descrie(self): | |
return f"Professor {self.name}, specializarea {self.specializare}" | |
class Department: | |
def __init__(self, nume): | |
self.nume = nume | |
#lista care contine o referinta catre obiecte Professor | |
self.professors = [] | |
def adauga_professor(self, professor): | |
self.professors.append( professor ) | |
def listeaza_profesori(self): | |
result = f"Department {self.name} include profesorii: \n" | |
for profesor in self.professors: | |
result += f"{profesor.descrie()}\n" | |
return result | |
def main(): | |
prof1 = Professor("John", "matematica") | |
prof1 = Professor("John", "info") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment