OOP and Inheritance : Inheritance : Class PYTHON TUTORIALS


PYTHON TUTORIALS » Class » Inheritance »

 

OOP and Inheritance


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 "<Employee: name=%s, salary=%s>" (self.name, self.salary)

class Developer(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 Tester(Developer):
    def __init__(self, name):
        Developer.__init__(self, name)
    def work(self):
        print self.name, "makes pizza"

bob = Tester('bob')       
print bob                     # Run inherited __repr__
bob.work(  )                  # Run type-specific action
bob.giveRaise(0.20)           # Give bob a 20% raise
print bob; print

for klass in Employee, Developer, Server, Tester:
    obj = klass(klass.__name__)
    obj.work(  )



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Class
» Inheritance