CS 535 Object-Oriented Programming Fall Semester, 2003 Classes 2 Appendix |
||
---|---|---|
© 2003, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 18-Sep-03 |
Smalltalk.Core defineClass: #Parent superclass: #{Core.Object} indexedType: #none private: false instanceVariableNames: 'foo ' classInstanceVariableNames: '' imports: '' category: 'CS535'
new ^super new initialize
initialize foo :=6.
foo ^foo
Initialization of Subclass
How to initialize bar?
Smalltalk.Core defineClass: #Child superclass: #{Core.Parent} indexedType: #none private: false instanceVariableNames: 'bar ' classInstanceVariableNames: '' imports: '' category: 'CS535'
Bad Idea 1 – Use Same pattern
Child class>>new ^super new initialize
Child>>initialize bar := 2.
Child>>bar ^bar
Why bad?
Bad Idea 2 – Subclass initializes Parent Variable
Child>>initialize bar := 2. foo := 6.
Child>>bar ^bar
Why Bad?
Child class now involved in private affairs of the Parent
Changes to the Parent instance variables require changing Child
Solution
Child>>initialize super initialize bar := 2.
Child>>bar ^bar
Copyright ©, All rights reserved.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 18-Sep-03    Next