49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
# 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()) |