Casting between parent and child classes in delphi

夙愿已清 提交于 2019-12-10 06:28:34

问题


I'm writing some software that targets two versions of very similar hardware which, until I use the API to initialize the hardware I'm not able to know which type I'll be getting back.

Because the hardware is very similar I planned to have a parent class (TParent) that has some abstract methods (for where the hardware differs) and then two child classes (TChildA, TChildB) which implement those methods in a hardware dependent manner.

So I would first instantiate an object of TParent check what kind it is then cast it to the correct child.

However when I do this and call one of the abstract methods fully implemented in the child class I get an EAbstractError.

e.g:

myHardware:=TParent.Create();

if myHardware.TypeA then
   myHardware:=TChildA(myHardware)
else
   myHardware:=TChildB(myHardware);

myHardware.SomeMehtod();

I'm assuming that I can't cast a Parent Class to a child class, and also that there's probably a better way of doing this. Any pointers?


回答1:


You're right, you can't and shouldn't cast from base class to derived class.

I'm assuming you don't want to have the Child object re-run the Parent constructor?

If so . . .

Remove the Parent/Child relationship as it stands, you will have only one Hardware class. For the specific ChildA and ChildB functionality, create a new inheritance pattern, so that you have an ISpecificHardwareTasks interface or base class, and two derived classes (SpecificA & SpecificB).

When Hardware is constructing it's self, and it gets to the point where it knows what type of hardware it's working with, it then creates an instance of SpecificA or SpecificB). This instance is private to Hardware.

Hardware exposes methods which wrap the ISpecificHardWareTasks methods (it can even implement that interface if that makes sense).

The Specific classes can take a reference to the Hardware class, if that's necessary (though I don't know if you have access to the this pointer in a constructor, my Delphi is getting rusty)

Hope these ramblings helped somewhat.




回答2:


You need a factory method to return you the correct class depending on the Type of hardware you are using...

function CreateHardware(isTypeA: Boolean): TParent;
begin
  if IsTypeA then Result := TChildA.Create
  else Result := TChildB.Create;
end;
...

var
  myHardware: TParent;
begin
  myHardware := CreateHardware(True);
  myHardwarde.SomeMethod;
end;

... or you could use the State pattern.

Common in either approach is that your TParent class does not has the knowledge to determine the type of hardware.That knowlegde is transfered into the factory method, caller of the factory method, factory itself or state class.




回答3:


Thanks to Binary Worrier and Mghie for pointing me in the right direction in this instance. The answer given by Lieven would be the easier way in cases where minimising the initialization of the hardware wasn't an issue.

The pImpl idiom is discussed elsewhere on SO

Here is how I understand the implementation in pseudo-delphi-code (note I've not bothered with public/private distinctions for this):

class TParent 
  procedure SomeMethod(); abstract;
end;

class TChildA (TParent)
  procedure SomeMethod(); override;
end;

class TChildB (TParent)
  procedure SomeMethod(); override;
end;

class THardware
 HardwareSpecficMethods: TParent;
 procedure SomeMethod;
 constructor Create();

contrsuctor THardware.Create();
begin
 InitializeHardware();
 If Hardware.TypeA then
  HardwareSpecificMethods:=TChildA.Create()
 else
  HardwareSpecificMethods:=TChildB.Create();
end;

procedure THardware.SomeMethod();
begin
  HardwareSpecificMethods.SomeMethod();
end;
end; {class THardware}


来源:https://stackoverflow.com/questions/681522/casting-between-parent-and-child-classes-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!