Last active
August 29, 2016 03:38
-
-
Save aduartem/158c6f94edce68ddfbbb to your computer and use it in GitHub Desktop.
Python - Clases y Objetos
This file contains hidden or 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
#!/usr/bin/python | |
"""Ejemplo extraido desde el libro Python para todos de Raul Gonzalez Duque""" | |
class Coche: | |
def __init__(self, gasolina): | |
self.gasolina = gasolina | |
print "Tenemos", gasolina, "litros" | |
def arrancar(self): | |
if self.gasolina > 0: | |
print "Arranca" | |
else: | |
print "No arranca" | |
def conducir(self): | |
if self.gasolina > 0: | |
self.gasolina -= 1 | |
print "Quedan", self.gasolina, "litros" | |
else: | |
print "No se mueve" | |
mi_coche = Coche(4) | |
mi_coche.arrancar() | |
mi_coche.conducir() | |
mi_coche.conducir() | |
mi_coche.conducir() | |
mi_coche.conducir() | |
mi_coche.arrancar() | |
print mi_coche.gasolina |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment