python类的组合应用

class Employee:
    def __init__(self,name,salary = 0):
        self.name = name
        self.salary = salary
    def giveRaise(self,percent):
        self.salary = self.salary + (self.salary * percent)
    def work(self):
        print self.name,' does stuff'
    def __repr__(self):
        return '' % (self.name,self.salary)
    
class Chef(Employee):
    def __init__(self,name):
        Employee.__init__(self,name,50000)
    def work(self):
        print self.name,'makes food'

class Server(Employee):
    def __init__(self,name):
        Employee.__init__(self,name,40000)
    def work(self):
        print self.name, ' interfaces with customer'
class PizzaRobot(Chef):
    def __init__(self,name):
        Chef.__init__(self,name)
    def work(self):
        print self.name,' makes pizza'
        

class Customer:
    def __init__(self,name):
        self.name = name
    def order(self,server):
        print self.name,' order from ',server
    def pay(self,server):
        print self.name,' pays for item to ', server

class Oven:
    def bake(self):
        print 'oven bakes'

class PizzaShop:
    def __init__(self):
        self.server = Server('Pat')
        self.chef = PizzaRobot('Bob')
        self.oven = Oven()
    
    def order(self,name):
        customer = Customer(name)
        customer.order(self.server)
        self.chef.work()
        self.oven.bake()
        customer.pay(self.server)
if __name__ == '__main__':
    scene = PizzaShop()
    scene.order('Homer')
    print '...'
    scene.order('Shaggy')

输出:

Homer  order from  
Bob  makes pizza
oven bakes
Homer  pays for item to  
...
Shaggy  order from  
Bob  makes pizza
oven bakes
Shaggy  pays for item to  


你可能感兴趣的:(python)