Article ID: 132359
Article Last Modified on 8/25/1999
*-- Note: All Print methods output to active window for demo.
CLEAR
? 'Example :'
DO PolyExample
RETURN
*-- All objects in this example are derived from the same ancestor, the
*-- File class.
*-- Polymorphism is demonstrated here by sending the Print method to three
*-- independent objects: a document, a spreadsheet, and a file. The Print
*-- method is performed for each object instance, demonstrating how each
*-- instance knows what action to perform.
PROCEDURE PolyExample
PRIVATE oMyFile1,oMyFile2,oMyFile3
oMyFile1=CREATEOBJECT('File')
oMyFile1.SetFileName('TEST1.TXT')
oMyFile1.Print
oMyFile2=CREATEOBJECT('DocumentFile')
oMyFile2.SetFileName('TEST2.DOC')
oMyFile2.Print
oMyFile3=CREATEOBJECT('SpreadsheetFile') && Creates the third object
oMyFile3.SetFileName('TEST3.XLS')
oMyFile3.Print
RETURN
*-- Base File class.
*-- Instance variable cFileName is protected for encapsulation.
*-- SetFileName(<expC>) method updates instance variable cFileName.
*-- Print method prints file based on instance variable cFileName.
DEFINE CLASS File AS Custom
PROTECTED cFileName
FUNCTION SetFileName
PARAMETERS lcNewFileName
this.cFileName=lcNewFileName
ENDFUNC
FUNCTION Print && performs the Print method for a File object
IF EMPTY(this.cFileName)
RETURN .F.
ENDIF
? 'Print File: '+this.cFileName
ENDFUNC
ENDDEFINE
*-- DocumentFile class derived from File class.
*-- Print method prints file based on instance variable cFileName. It
*-- overwrites Print method defined in the File class.
DEFINE CLASS DocumentFile AS File
FUNCTION Print
IF EMPTY(this.cFileName)
RETURN .F.
ENDIF
? 'Document File: '+this.cFileName
ENDFUNC
ENDDEFINE
*-- SpreadsheetFile class derived from File class.
DEFINE CLASS SpreadsheetFile AS File
FUNCTION Print
IF EMPTY(this.cFileName)
RETURN .F.
ENDIF
? 'Spreadsheet File: '+this.cFileName
ENDFUNC
ENDDEFINE
Additional query words: message VFoxWin
Keywords: KB132359