AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Need create and call method from many class types
Thema durchsuchen
Ansicht
Themen-Optionen

Need create and call method from many class types

Ein Thema von WojTec · begonnen am 6. Jan 2011 · letzter Beitrag vom 10. Jan 2011
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#1

Need create and call method from many class types

  Alt 6. Jan 2011, 20:26
Delphi-Version: 2010
Hello, guys,

So, lets say what I have.

I have two classes:
- TClass1(TBaseClass)
- TClass2(BaseClass)

TClass1 and TClass2 extands TBaseClass, both are for different input data types. TClass1/TClass2 has LoadFromStream and SaveToStream.

Now, how to create and call methods with names that are common for any class TSomeOtherClass(TClass1/TClass2), like stream procedures?

Currently I have:

if class = someclass then someclass.loadfromstream
else if class = otherclass then otherclass.loadfromstream
else if class = yetanotherclass then yetanotherclass.loadfromstream

but in future I'll have too many classes to add if statement in few places in few units in program.

Could you help with this?
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.861 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Need create and call method from many class types

  Alt 6. Jan 2011, 20:32
Why not if class is TBaseclass then BaseClass( class).loadfromstream()); ?
Markus Kinzler
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#3

Re: Need create and call method from many class types

  Alt 6. Jan 2011, 20:51
And this will call correct method (someclass.loadfromstream)?
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.861 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Need create and call method from many class types

  Alt 6. Jan 2011, 20:56
Yes, if BaseClass is a superclass of class
Markus Kinzler
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Need create and call method from many class types

  Alt 6. Jan 2011, 21:15
What it mean? This is somethink like this (I saw somewhere, but don't know how it working):

TClassOfBase = class of TBaseClass

Delphi-Quellcode:
TBaseClass = class;
TSomeClassA = class(TBaseClass);
TSomeClassB = class(TBaseClass);

//(TSomeClassA and TSomeClassB) are base for any class I'm working with it).

I have TClass1(TSomeClassA), TClass2(TSomeClassB), etc. How it should be done to avoid many thousends if-then-else?

Could you explain me, because I don't understand?

Geändert von WojTec ( 6. Jan 2011 um 21:20 Uhr)
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.861 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Need create and call method from many class types

  Alt 6. Jan 2011, 21:29
Because of the IS-A paradigm each Subclass "is a" Baseclass

if the wanted function/procedure is declared in a superclass ( baseclass->MiddleClass->Class1) it can be called as function/procedure of the superclass. It'S always called the "best matched" function.
if the function is implemented in the Baaseclass this function is called, if the implementation is in Class1/Class2 then the corresponding versions are used:
TBaseClass(class1).AnyFunc() calls Tclass1.AnyFunc()
Markus Kinzler
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#7

AW: Need create and call method from many class types

  Alt 7. Jan 2011, 09:54
If you want to figure out how the "Class of Class" thing works, just try to understand the TPicture.LoadFromFile method, which can be found in the "graphics" unit.
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG
  Mit Zitat antworten Zitat
Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.027 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#8

AW: Need create and call method from many class types

  Alt 10. Jan 2011, 12:52
TBaseClass has to have LoadFromStream as virtual (abstract).
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:49 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz