




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Leonard Bruns, [email protected] Robotics, Perception and Learning, KTH
(^) Object oriented programming (^) Classes, objects and instances (^) Inheritance (^) Polymorphism
(^) Exercise / Example
Inheritance
(^) Inheritance allows to model parent → child hierarchies (^) Always ask, whether such a hierarchy is actually required or useful (^) Inherent hierarchical structure (^) Additional variables and/or more specific functions required for children (^) Differences cannot easily be handled with a variable
Person
Customer
MaleEmployee FemaleEmployee
Employee
Inheritance
class Person () : def init ( self , name ) : self. name = name class Employee ( Person ) : def init ( self , name , salary ) : Person. init ( self , name ) self. salary = salary class Customer ( Person ) : def init ( self , name , purchases ) : Person. init ( self , name ) self. purchases = purchases
Person
Customer Employee
Polymorphism
(^) Base class / parent often describes interface (^) On a list of parent/child instances, we can use parent functions for all of them
persons = [] persons. append ( Person ( ' Peter ') ) persons. append ( Employee ( ' Ludvig ' , salary =30000) ) persons. append ( Customer ( ' Daniel ' , purchases =[]) )
for person in persons : person. print ()
Person
Customer Employee
(^) Design a zoo managing program