







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of inheritance and subclasses in object-oriented programming using the example of a drawing program. It covers defining classes for different types of slide content, creating subclasses, and the importance of names in subclasses and superclasses. The document also discusses customizing classes and the process of initialization.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








init(x,y,w,h) draw_frame() select()
init(x,y,text) draw() TextBox(SC) TextBox text ‘Hi!’ id p (^) id p.text (^) p.draw() p.select()
class A(object): x = 29 y = 42 def init(self): self.y = 2 self.z = 3 def f(self): print 'this is A.f' print 'self.x:', self.x print 'self.y', self.y print 'self.z', self.z print 'A.y', A.y a = A() print 'a.y:', a.y print 'A.y:', A.y a.f() A.f(a) which appears? (A) a.y: 42 (B) a.y: 29 (C) a.y: 2 (D) an error which appears? (A) A.y: 42 (B) A.y: 29 (C) A.y: 2 (D) an error which appears? (A) self.y: 42 (B) self.y: 29 (C) self.y: 2 (D) an error The two calls (A) do the same thing to A.f: (B) first is an error (C) second is an error (D) there are not two calls
class SlideContent(object): """Any object on a slide.""" def init(self, x, y, w, h): """Obj. with given pos'n and size""" self.x = x; self.y = y self.w = w; self.h = h … class TextBox(SlideContent): """An object containing text.""" def init(self, x, y, text): w = width(text) h = height(text) SlideContent.init(self, x, y, w, h) self.text = text … class Image(SlideContent): """An image.""" def init(self, x, y, image_file): … … SlideContent initializer sets up instance variables for the position and size of the object on the slide TextBox initializer overrides the superclass initializer. Only the TextBox initializer is called to initialize a fresh TextBox instance. To ensure that the superclass still gets initialized, the subclass initializer must call the superclass initializer explicitly. In this example the size is computed in the initializer.