L'OO selon St Python

De Wiki Linux62.

L'explication

<python> t'as jms fait de prog orientee objet ?

<Jojosan> jamais.

<python> hum okp

<python> donc tu te galeres avec le concept de classe/methodes ?

<Jojosan> ouaip

<python> tu veux que je t'explique en ultra resume ?

<Jojosan> résume largement :)

<python> okp

<Jojosan> ais que j'sois fonctionnel à peu près

<python> disons qu'une classe ca va representer un objet

<python> par exemple

<python> tu peux modeliser un etre humain par une classe

<python> cette classe aura des attributs (par exemple la taille en cm, le poids etc...)

<python> et des methodes

<python> que l'objet effectue

<Jojosan> okep.

<python> par exemple: boire(), manger(), faire_caca(), grossir(), taper_son_voisin() etc..

<python> voila

<python> ensuite t'as l'heritage

<python> tu peux _specialiser_ la classe Humain

<python> en 2 sous-classes: Homme et Femme

<Jojosan> okip

<python> donc la classe Homme herite de la classe Humain

<Jojosan> qui peuvent toujours faire_caca() etc... :]

<python> donc un Homme a toutes les methodes et attributs de Humain (boire(), manger() etc...)

<python> voila

<python> mais t'as des trucs en plus chez l'Homme que l'Humain n'a pas

<Jojosan> okep

<python> par exemple, dans les attributs de l'Homme, tu peux rajouter la taille du sexe

<Jojosan> I see :)

<python> et dans la classe Femme, qui herite d'Humain, tu peux mettre comme attribut la taille des seins, ou encore une nouvelle methode: Accoucher()

<python> etc... :)

<python> ca c'est le concept de classe ;)

<python> donc t'as

<python> class Humain:

<python> etc...

<python> et t'as une fonction d'initialisation

<python> qui est appellee a la creation de l'objet

<python> cad quand tu fais: bob = Humain()

<python> c'est la fonction __init__(self)

<Jojosan> okep

<python> par exemple

<Jojosan> ah c'est juste ça :|

<python> si pour init ton humain faut donner la taille et le poids

<python> t'auras

<python> class Humain:

<python> def __init__(self, taille, poids):

<python> self.size = taille

<python> self.weight = poids

<python> par exemple

<Jojosan> okies :)

<python> et apres pour creer un Humain tu fais: bob = Humain(183, 82)

<python> voilou :)

<python> et le "self" tu maitrises ?

<Jojosan> j'ai l'idée de ce que c'est

<python> okp

<Jojosan> ça pointe vers l'appellant en gros ?

<python> ca pointe sur la classe dans laquelle tu es

<python> ui

<Jojosan> okip

<python> toutes les methodes ont au moins un argument qui est self

<python> par exemple dans la classe Humain

<python> def boire(self, liquide):

<python> ....

<python> et tu feras: bob.boire("eau")

<python> ou alors

<python> def faire_caca(self):

<python> ...

<Jojosan> okie

<python> et tu feras: bob.faire_caca()

<python> ok bob ?

<Jojosan> ok bud

<python> good :)

<Jojosan> hey

<Jojosan> easy :)

<python> tu maitrises les classes en python ;)


Le code

#!/usr/bin/python <br /> <br /> class Humain:<br /> def __init__(self, taille, poids):<br /> # init des attributs<br /> self.height = taille<br /> self.weight = poids<br /> def boire(self, boisson):<br /> print "Je bois " + boisson<br /> <br /> def manger(self, plat):<br /> print "Je mange " + plat<br /> <br /> def dormir(self):<br /> print "zZzzZZzzZZ"<br /> <br /> class Homme(Humain):<br /> def __init__(self, taille, poids, taille_sexe):<br /> Humain.__init__(self, taille, poids) # on init la classe mere<br /> self.sex_length = taille_sexe<br /> <br /> class Femme(Humain):<br /> def __init__(self, taille, poids, poitrine):<br /> Humain.__init__(self, taille, poids) # on init la classe mere<br /> self.tour_poitrine = poitrine<br /> <br /> def accoucher(self):<br /> print "Ouh ca fait mal !"<br /> <br /> # Creations de 3 instances<br /> <br /> a = Humain(181, 81)<br /> bob = Homme(178,65,44)<br /> bobette = Femme(164, 54, 90)<br /> <br /> # Diverses actions pour l'Humain<br /> <br /> a.boire("eau")<br /> <br /> a.manger("chocolat")<br /> <br /> a.dormir()<br /> <br /> # Homme herite de Humain, donc Homme peut faire tout ce qu'un Humain peut faire, par exemple: dormir()<br /> <br /> bob.dormir()<br /> <br /> # De la meme facon, une Femme peut manger()<br /> <br /> bobette.manger("artichaut")<br /> <br /> bobette.accoucher()<br />

Outils personnels
Équipes