delphi - Inheritance/polymorphism concept -
I have two binary files that have the same type of data, so I have an integrated viewer Audience) Some, the methods are common to these two file types, there is nothing, so I have created a base class tshap, and it contains ticircle and teetering.
Pseudo-code:
TShape = class (TObject) function NoOfItems: integer; Virtual; abstract; End; TCircle = class (tshape) function no off items: integer; Override; & Lt; ---- Actual implementation ends; TITRING = Class (TESPE) function NO OF ITEMS: integer; Override; & Lt; ---- Actual implementation ends; TViewer = Class (TStringGrid) container: TShape; End;
And I use it like this:
process main; If FileType = Circle starts (viewer as TCircle.). Load (FileName) Other (in the form of viewer TTriangle). Load (FileName); Caption: = Viewer Container. Nobody; & Lt; ---- It calls TShape which is an intangible end;
When I do this, it works:
If viewer. Container is TTriangle then caption: = (Viewer.Container as TTriangle) .NoOfItems else ...
But I want to do it straightaway like this:
caption: = viewer Container. Nobody; There is nothing wrong in using is
clearly, except that I have to use it in many places (close to everywhere). Is this a good way to get an integrated viewer? Update:
Actually, this may also be a display problem. My files actually have a large number of items (up to billions), so so many '/ in' tests can actually affect the speed.
You're doing it wrong.
You have to change your code so that the container will not be made until you know what type of requirement it is, and then make the appropriate type:
Process Main; If the file type = circle then the viewer. Start: Container: = Ticircle. Create other viewers. Container: = Teatangel.Creat; Viewer.Container.Load (file name); Caption: = IntToStr (viewer, container, none); & Lt; ---- It calls the appropriate code end;
Here is an example of using inheritance and polymorphism for you:
Program inheritance inheritance polymorphism; Uses System.SysUtils; Type Tien = square public process; Virtual; Speak Process; Virtual; End; Tidog = class (tanimal) public process sit; Override; Speak Process; Override; End; TCAT = Speak (Tienal) Public Process; Override; End; Tianamalare = array of tienal; {TCAT} Process TCT. say; inherited from; WriteLn ('Bah! There's no way to speak when cats speak.'); End; {TDog} process TDog.Sit; inherited from; WriteLn ('sitting down'.); End; Process TDog.Speak; inherited from; Written ('wah! Wow!'); End; Process TAnimal.Sit; Initial end; TAnimal.Speak the process; Initial end; Various Animals: Tianam Aurere; I: integer; Pet: Tannim; {TAnimal} number NumAnimals = 5; Start Setland (Animals, Numbers); I: = 0 to higher (animal) if odd (i) then animal [i]: = TDog.Create else Animals [i]: = TCat.Create; End; Pet start to pet. say; Pet.Sit; End; Writeln (''); Readln; End.
Comments
Post a Comment