Last active
July 5, 2017 14:17
-
-
Save JokerMartini/bb807c90fb4427a3a4b1 to your computer and use it in GitHub Desktop.
Python: Simple example of class inheritance
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
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() |
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
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