Created
October 24, 2019 16:12
-
-
Save Bigaston/d1914af51706b565164221b67167c755 to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
class Humain: | |
TYPE_SEXE = ["F", "H", "A"] | |
les_humains = [] | |
#Constructeur | |
def __init__(self, nom, prenom, sexe, age): | |
#Vérification de la valeure du sexe | |
if (sexe not in Humain.TYPE_SEXE): | |
raise ValueError("Le sexe ne peut que être F, H ou A") | |
#Vérification de l'age | |
if (age < 0): | |
raise ValueError("L'âge ne peut pas être négatif") | |
self.nom = nom | |
self.prenom = prenom | |
self.sexe = sexe | |
self.age = age | |
Humain.les_humains.append(self) | |
#Méthode spéciale en cas d'adition | |
def __add__(self, other): | |
s = random.randint(0,2) | |
p = random.randint(0,1) | |
parent = [self, other] | |
return Humain(parent[p].nom, self.prenom + " " + other.prenom, Humain.TYPE_SEXE[s], 0) | |
#Méthode pour afficher la classe dans la console ou en str() | |
def __str__(self): | |
return "Humain : " + self.prenom + " " + self.nom + " (" + str(self.age) + " ans) [" + self.sexe + "]" | |
#Para | |
#Methodes d'instances | |
def sePresenter(self): | |
print("Bonjour! Je suis " + self.prenom + " " + self.nom + "! Je suis un " + self.sexe + " et j'ai " + str(self.age) + " ans") | |
def anniversaire(self): | |
self.age+=1 | |
print(self.prenom + " fête son anniversaire! Il a " + str(self.age) + " ans!") | |
#Méthode de classe | |
def nouvelAn(cls): | |
print("Une année passe!") | |
for h in cls.les_humains: | |
h.anniversaire() | |
nouvelAn = classmethod(nouvelAn) | |
michel = Humain("Paul", "Michel", "H", 23) | |
michel.sePresenter() | |
micheline = Humain("Robertos", "Micheline", "F", 55) | |
micheline.sePresenter() | |
gosse = michel + micheline | |
gosse.sePresenter() | |
gosse.anniversaire() | |
Humain.nouvelAn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment