Download presentation
Presentation is loading. Please wait.
1
polymorphism Inheritance Base class Derived class
2
recall from last class -polymorphism
r = a rectangle t = a triangle c = a circle txt = some text shapeList=[r,t,c,txt] for shape in shapeList: shape.draw(win)
3
Inheritance mammal, horse, kangaroo mammal horse kangaroo
4
Inheritance mammal, horse, kangaroo mammal backbone fur mammal horse
4 legs tail kangaroo backbone fur pocket tail horse kangaroo
5
Inheritance Inheritance :derive a new class from an existing one
existing class:the parent class, or superclass, or base class The new class: derived class ,the child class or subclass the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class Copyright © 2012 Pearson Education, Inc.
6
is-a relationship get all the member variables get all the member methods if you want. can add more variables can add or modify methods software reuse is good – already tested, etc.
7
Base class: Animal class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
8
Base class: Animal - constructor
class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies Has instance data self.name (a string) self.flies(boolean, default is False)
9
Base class: Animal – other methods
def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, # unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
10
Derived class: Duck class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers
11
Derived class: Duck - constructor
class Duck(Animal): #The derived constructor calls # the base constructor, # then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers instance data self.name (from Animal) self.flies (from Animal) self.feathers (just for Duck)
12
Derived class: Duck – other methods
#This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk totally overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")
13
main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move()
14
class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
15
main def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() just an animal self.name False self.flies a just an animal Indistinct animal noises Eat, eat, eat.
16
class Animal: #base class def __init__(self,name="just an animal",flies=False): self.name=name self.flies=flies def display(self): print(self.name, end="") def talk(self): print("Indistinct animal noises") #This eat method will be used, unchanged, for Duck. def eat(self): print("Eat, eat, eat.")
17
class Duck(Animal): #The derived constructor calls the base constructor, #then does more def __init__(self, feathers): Animal.__init__(self,"duck") self.flies=True self.feathers=feathers #This display does more than the animal display, #but makes use of Animal.display def display(self): print("I am a", end=" ") Animal.display(self) print(" with ",self.feathers, " feathers") #This talk total overrides the Animal talk def talk(self): print("Quack! Quack!") # Animal has no move method. Only for Ducks. def move(self): print("I swim. I fly. I walk")
18
main just an animal Indistinct animal noises Eat, eat, eat. I am a duck with feathers Quack! Quack! I swim. I fly. I walk def main(): a=Animal() a.display() print() a.talk() a.eat() d=Duck(1000) d.display() d.talk() d.eat() d.move() a just an animal self.name False self.flies 1000 self.feathers duck self.name False True self.flies d
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.