L'OO selon St Python
De Wiki Linux62.
(10 révisions intermédiaires par 3 utilisateurs sont masquées) | |||
Ligne 1 : | Ligne 1 : | ||
− | <python> t'as | + | == L'explication == |
+ | |||
+ | <python> t'as jamais fait de programmation orientée objet ? | ||
+ | |||
<Jojosan> jamais. | <Jojosan> jamais. | ||
− | <python> hum | + | |
− | <python> donc tu | + | <python> hum ok |
+ | |||
+ | <python> donc tu galères avec le concept de classe/methodes ? | ||
+ | |||
<Jojosan> ouaip | <Jojosan> ouaip | ||
− | <python> tu veux que je t'explique en ultra | + | |
− | + | <python> tu veux que je t'explique en ultra résumé ? | |
+ | |||
<Jojosan> résume largement :) | <Jojosan> résume largement :) | ||
− | <python> | + | |
− | <Jojosan> | + | <python> ok |
+ | |||
+ | <Jojosan> fais que j'sois fonctionnel à peu près | ||
+ | |||
<python> disons qu'une classe ca va representer un objet | <python> disons qu'une classe ca va representer un objet | ||
+ | |||
<python> par exemple | <python> par exemple | ||
+ | |||
<python> tu peux modeliser un etre humain par une classe | <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> cette classe aura des attributs (par exemple la taille en cm, le poids etc...) | ||
+ | |||
<python> et des methodes | <python> et des methodes | ||
+ | |||
<python> que l'objet effectue | <python> que l'objet effectue | ||
+ | |||
<Jojosan> okep. | <Jojosan> okep. | ||
+ | |||
<python> par exemple: boire(), manger(), faire_caca(), grossir(), taper_son_voisin() etc.. | <python> par exemple: boire(), manger(), faire_caca(), grossir(), taper_son_voisin() etc.. | ||
+ | |||
<python> voila | <python> voila | ||
+ | |||
<python> ensuite t'as l'heritage | <python> ensuite t'as l'heritage | ||
+ | |||
<python> tu peux _specialiser_ la classe Humain | <python> tu peux _specialiser_ la classe Humain | ||
+ | |||
<python> en 2 sous-classes: Homme et Femme | <python> en 2 sous-classes: Homme et Femme | ||
+ | |||
<Jojosan> okip | <Jojosan> okip | ||
+ | |||
<python> donc la classe Homme herite de la classe Humain | <python> donc la classe Homme herite de la classe Humain | ||
+ | |||
<Jojosan> qui peuvent toujours faire_caca() etc... :] | <Jojosan> qui peuvent toujours faire_caca() etc... :] | ||
+ | |||
<python> donc un Homme a toutes les methodes et attributs de Humain (boire(), manger() etc...) | <python> donc un Homme a toutes les methodes et attributs de Humain (boire(), manger() etc...) | ||
+ | |||
<python> voila | <python> voila | ||
+ | |||
<python> mais t'as des trucs en plus chez l'Homme que l'Humain n'a pas | <python> mais t'as des trucs en plus chez l'Homme que l'Humain n'a pas | ||
+ | |||
<Jojosan> okep | <Jojosan> okep | ||
+ | |||
<python> par exemple, dans les attributs de l'Homme, tu peux rajouter la taille du sexe | <python> par exemple, dans les attributs de l'Homme, tu peux rajouter la taille du sexe | ||
+ | |||
<Jojosan> I see :) | <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> 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> etc... :) | ||
+ | |||
<python> ca c'est le concept de classe ;) | <python> ca c'est le concept de classe ;) | ||
+ | |||
<python> donc t'as | <python> donc t'as | ||
+ | |||
<python> class Humain: | <python> class Humain: | ||
+ | |||
<python> etc... | <python> etc... | ||
+ | |||
<python> et t'as une fonction d'initialisation | <python> et t'as une fonction d'initialisation | ||
+ | |||
<python> qui est appellee a la creation de l'objet | <python> qui est appellee a la creation de l'objet | ||
+ | |||
<python> cad quand tu fais: bob = Humain() | <python> cad quand tu fais: bob = Humain() | ||
+ | |||
<python> c'est la fonction __init__(self) | <python> c'est la fonction __init__(self) | ||
+ | |||
<Jojosan> okep | <Jojosan> okep | ||
+ | |||
<python> par exemple | <python> par exemple | ||
+ | |||
<Jojosan> ah c'est juste ça :| | <Jojosan> ah c'est juste ça :| | ||
+ | |||
<python> si pour init ton humain faut donner la taille et le poids | <python> si pour init ton humain faut donner la taille et le poids | ||
+ | |||
<python> t'auras | <python> t'auras | ||
+ | |||
<python> class Humain: | <python> class Humain: | ||
+ | |||
<python> def __init__(self, taille, poids): | <python> def __init__(self, taille, poids): | ||
+ | |||
<python> self.size = taille | <python> self.size = taille | ||
+ | |||
<python> self.weight = poids | <python> self.weight = poids | ||
+ | |||
<python> par exemple | <python> par exemple | ||
+ | |||
<Jojosan> okies :) | <Jojosan> okies :) | ||
+ | |||
<python> et apres pour creer un Humain tu fais: bob = Humain(183, 82) | <python> et apres pour creer un Humain tu fais: bob = Humain(183, 82) | ||
+ | |||
<python> voilou :) | <python> voilou :) | ||
+ | |||
<python> et le "self" tu maitrises ? | <python> et le "self" tu maitrises ? | ||
+ | |||
<Jojosan> j'ai l'idée de ce que c'est | <Jojosan> j'ai l'idée de ce que c'est | ||
+ | |||
<python> okp | <python> okp | ||
+ | |||
<Jojosan> ça pointe vers l'appellant en gros ? | <Jojosan> ça pointe vers l'appellant en gros ? | ||
+ | |||
<python> ca pointe sur la classe dans laquelle tu es | <python> ca pointe sur la classe dans laquelle tu es | ||
+ | |||
<python> ui | <python> ui | ||
+ | |||
<Jojosan> okip | <Jojosan> okip | ||
+ | |||
<python> toutes les methodes ont au moins un argument qui est self | <python> toutes les methodes ont au moins un argument qui est self | ||
+ | |||
<python> par exemple dans la classe Humain | <python> par exemple dans la classe Humain | ||
+ | |||
<python> def boire(self, liquide): | <python> def boire(self, liquide): | ||
+ | |||
<python> .... | <python> .... | ||
+ | |||
<python> et tu feras: bob.boire("eau") | <python> et tu feras: bob.boire("eau") | ||
+ | |||
<python> ou alors | <python> ou alors | ||
+ | |||
<python> def faire_caca(self): | <python> def faire_caca(self): | ||
+ | |||
<python> ... | <python> ... | ||
+ | |||
<Jojosan> okie | <Jojosan> okie | ||
+ | |||
<python> et tu feras: bob.faire_caca() | <python> et tu feras: bob.faire_caca() | ||
+ | |||
<python> ok bob ? | <python> ok bob ? | ||
+ | |||
<Jojosan> ok bud | <Jojosan> ok bud | ||
+ | |||
<python> good :) | <python> good :) | ||
+ | |||
<Jojosan> hey | <Jojosan> hey | ||
+ | |||
<Jojosan> easy :) | <Jojosan> easy :) | ||
+ | |||
<python> tu maitrises les classes en python ;) | <python> tu maitrises les classes en python ;) | ||
+ | |||
+ | |||
+ | == Le code == | ||
+ | |||
+ | #!/usr/bin/python | ||
+ | |||
+ | class Humain: | ||
+ | def __init__(self, taille, poids): | ||
+ | # init des attributs | ||
+ | self.height = taille | ||
+ | self.weight = poids | ||
+ | |||
+ | def boire(self, boisson): | ||
+ | print "Je bois " + boisson | ||
+ | |||
+ | def manger(self, plat): | ||
+ | print "Je mange " + plat | ||
+ | |||
+ | def dormir(self): | ||
+ | print "zZzzZZzzZZ" | ||
+ | |||
+ | class Homme(Humain): | ||
+ | def __init__(self, taille, poids, taille_sexe): | ||
+ | Humain.__init__(self, taille, poids) # on init la classe mere | ||
+ | self.sex_length = taille_sexe | ||
+ | |||
+ | class Femme(Humain): | ||
+ | def __init__(self, taille, poids, poitrine): | ||
+ | Humain.__init__(self, taille, poids) # on init la classe mere | ||
+ | self.tour_poitrine = poitrine | ||
+ | |||
+ | def accoucher(self): | ||
+ | print "Ouh ca fait mal !" | ||
+ | |||
+ | # Creations de 3 instances | ||
+ | |||
+ | a = Humain(181, 81) | ||
+ | bob = Homme(178,65,44) | ||
+ | bobette = Femme(164, 54, 90) | ||
+ | |||
+ | # Diverses actions pour l'Humain | ||
+ | |||
+ | a.boire("eau") | ||
+ | a.manger("chocolat") | ||
+ | a.dormir() | ||
+ | |||
+ | # Homme herite de Humain, donc Homme peut faire tout ce qu'un Humain peut faire, par exemple: dormir() | ||
+ | bob.dormir() | ||
+ | |||
+ | # De la meme facon, une Femme peut manger() | ||
+ | bobette.manger("artichaut") | ||
+ | bobette.accoucher() |
Version actuelle en date du 8 octobre 2007 à 19:22
L'explication
<python> t'as jamais fait de programmation orientée objet ?
<Jojosan> jamais.
<python> hum ok
<python> donc tu galères avec le concept de classe/methodes ?
<Jojosan> ouaip
<python> tu veux que je t'explique en ultra résumé ?
<Jojosan> résume largement :)
<python> ok
<Jojosan> fais 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 class Humain: def __init__(self, taille, poids): # init des attributs self.height = taille self.weight = poids def boire(self, boisson): print "Je bois " + boisson def manger(self, plat): print "Je mange " + plat def dormir(self): print "zZzzZZzzZZ" class Homme(Humain): def __init__(self, taille, poids, taille_sexe): Humain.__init__(self, taille, poids) # on init la classe mere self.sex_length = taille_sexe class Femme(Humain): def __init__(self, taille, poids, poitrine): Humain.__init__(self, taille, poids) # on init la classe mere self.tour_poitrine = poitrine def accoucher(self): print "Ouh ca fait mal !" # Creations de 3 instances a = Humain(181, 81) bob = Homme(178,65,44) bobette = Femme(164, 54, 90) # Diverses actions pour l'Humain a.boire("eau") a.manger("chocolat") a.dormir() # Homme herite de Humain, donc Homme peut faire tout ce qu'un Humain peut faire, par exemple: dormir() bob.dormir() # De la meme facon, une Femme peut manger() bobette.manger("artichaut") bobette.accoucher()