Semaine 4, jour 4, soir
This commit is contained in:
49
Semaine_04/Fonctions/shapes.py
Normal file
49
Semaine_04/Fonctions/shapes.py
Normal file
@@ -0,0 +1,49 @@
|
||||
# Composition: traduit une relation "A UN" "POSSED UN" "DEPEND DE"
|
||||
from math import pi
|
||||
|
||||
class Point:
|
||||
def __init__(self, x: float, y: float):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
class Forme: # Abstraction
|
||||
def __init__(self, centre: Point):
|
||||
self.centre = centre
|
||||
|
||||
def aire(self):
|
||||
raise NotImplementedError("Attention aire doit etre redefini")
|
||||
|
||||
def perimetre(self):
|
||||
raise NotImplementedError("Attention perimetre doit etre redefini")
|
||||
|
||||
class Cercle(Forme):
|
||||
def __init__(self, centre: Point, rayon: float):
|
||||
super().__init__(centre)
|
||||
self.rayon = rayon
|
||||
|
||||
def aire(self):
|
||||
return pi * self.rayon ** 2
|
||||
|
||||
def perimetre(self):
|
||||
return 2 * pi * self.rayon
|
||||
|
||||
class Rectangle(Forme):
|
||||
def __init__(self, centre: Point, longeur: float, largeur: float):
|
||||
super().__init__(centre)
|
||||
self.longueur = longeur
|
||||
self.largeur = largeur
|
||||
|
||||
def aire(self):
|
||||
return self.longueur * self.largeur
|
||||
|
||||
def perimetre(self):
|
||||
return 2 * (self.longueur + self.largeur)
|
||||
|
||||
cercle1 = Cercle(centre= Point(2, 2), rayon= 5)
|
||||
cercle2 = Cercle(centre= Point(-2, 2), rayon= 6)
|
||||
rect1 = Rectangle(centre= Point(0, 0), longeur= 12, largeur= 5)
|
||||
rect2 = Rectangle(centre= Point(1, 1), longeur= 10, largeur= 6)
|
||||
|
||||
formes = [cercle1, cercle2, rect1, rect2]
|
||||
for forme in formes:
|
||||
print(forme.aire())
|
||||
Reference in New Issue
Block a user