class Person(object): """Base class for people""" def __init__(self, id, name, age, sex): self.__id = id self.__name = name self.__age = age self.__sex = sex def __repr__(self): return "<Person %s %s" % (self.__id, self.__name) def get_info(self): return "%s %s %s" % (self.__name, self.__age, self.__sex) def get_soft_drink(self): print "Getting a drink for", self.__name class Guest(Person): """Guest class, must provide a name and room""" def __init__(self, id, name, age, sex, room): Person.__init__(self, id, name, age, sex) self.__room = room class Staff(Person): """Staff, able to get free soft drinks and restock drinks but unable to purchase alcohol drinks""" def __init__(self, id, name, age, sex): Person.__init__(self, id, name, age, sex) def restock(self, dm): """Restock a drink machine, return string""" return dm.restock() class DrinkMachine(object): """Drink machine that uses a fridge and a dispenser""" def __init__(self): self.fridge = Fridge() self.disp = Dispenser() def restock(self): print "Restocking" def dispenseSoft(self): print "Dispensing soft..." def dispenseAlc(self): print "Dispensing alcohol..." class Fridge(object): """Fridge used by the Drink Machine to cool drinks""" pass class Dispenser(object): """Dispenser used by the Drink Machine to dispense drinks""" pass g = Guest(2, "Scott", "18", "M", "1A") g.get_soft_drink() dm = DrinkMachine() s = Staff(3, "Tim", "18", "M") s.restock(dm)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question