Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Last active July 5, 2017 14:17
Show Gist options
  • Save JokerMartini/bb807c90fb4427a3a4b1 to your computer and use it in GitHub Desktop.
Save JokerMartini/bb807c90fb4427a3a4b1 to your computer and use it in GitHub Desktop.
Python: Simple example of class inheritance
import sys
class Animal:
def __init__(self):
print "Animal created"
def whoAmI(self):
print "Animal"
def eat(self):
print "Eating"
class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print "Dog created"
def whoAmI(self):
print "Dog"
def bark(self):
print "Woof!"
d = Dog()
d.whoAmI()
d.eat()
d.bark()
class Person:
def __init__(self, first, last):
self.firstname = first
self.lastname = last
def Name(self):
return self.firstname + " " + self.lastname
class Employee(Person):
def __init__(self, first, last, staffnum):
Person.__init__(self,first, last)
self.staffnumber = staffnum
def GetEmployee(self):
return self.Name() + ", " + self.staffnumber
x = Person("Marge", "Simpson")
y = Employee("Homer", "Simpson", "1007")
print(x.Name())
print(y.GetEmployee())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment