jeudi 29 mai 2014

la surcharge de Delphi, substitution, méthode virtuelle - Stack Overflow


have simple object hierarchy like below


TLiveThing=class
protected
FTest:string;
constructor Create(whereLive:string);overload;virtual;
constructor Create(haveBone:Boolean);overload;virtual;
end;

THuman=class(TLiveThing)
public
constructor Create(whereLive:string);overload;override;
constructor Create(age:integer);overload;
end;

in theoretically, if I instantiate THuman, I have 2 constructor, but in fact I have 5 constructor displayed by code insight, actually I want to see 3 constructor, - Create(whereLive:String), overriden - Create(age:integer) - Create(haveBone:integer)


human:=THuman.Create(       <=====in there I have 5 suggestion constructor

why I have this strange behaviour? how to fix it, because it so anoying, I cant always check my class that I need to instantiate, and if I instantiate like below


human:=THuman.Create(); <===== it doesnt give me error

how I completely hide my anchestor constructor? , because if I instatiate like above, completely give me a wrong object


UPDATE: and also I can see default Create without parameter from TObject too




Without putting focus on your bad constructor implementation,


your problem is that both the ancestor and the child class are defined in the same unit, therefor the standard definition of Private/Protected does not apply here.


If you want to prevent the ancestor constructor (which you are overriding in the child class) from showing up as a code parameter when instantiating an object of that derrived class then simply make it a member of the strict protected or strict private section.


With your example :


TLiveThing=class
strict protected
constructor Create(whereLive:string); virtual;
end;

THuman=class(TLiveThing)
public
constructor Create(whereLive:string); overload; override;
constructor Create(age:integer); overload;
end;

This will prevent the ancestor constructor Create(whereLive:string) from showing up as a parameter when you are creating an instance of your child class.


As pointed out by David, this has nothing to do with hiding the default Create constructor, it's only viable for hiding your custom constructors.



have simple object hierarchy like below


TLiveThing=class
protected
FTest:string;
constructor Create(whereLive:string);overload;virtual;
constructor Create(haveBone:Boolean);overload;virtual;
end;

THuman=class(TLiveThing)
public
constructor Create(whereLive:string);overload;override;
constructor Create(age:integer);overload;
end;

in theoretically, if I instantiate THuman, I have 2 constructor, but in fact I have 5 constructor displayed by code insight, actually I want to see 3 constructor, - Create(whereLive:String), overriden - Create(age:integer) - Create(haveBone:integer)


human:=THuman.Create(       <=====in there I have 5 suggestion constructor

why I have this strange behaviour? how to fix it, because it so anoying, I cant always check my class that I need to instantiate, and if I instantiate like below


human:=THuman.Create(); <===== it doesnt give me error

how I completely hide my anchestor constructor? , because if I instatiate like above, completely give me a wrong object


UPDATE: and also I can see default Create without parameter from TObject too



Without putting focus on your bad constructor implementation,


your problem is that both the ancestor and the child class are defined in the same unit, therefor the standard definition of Private/Protected does not apply here.


If you want to prevent the ancestor constructor (which you are overriding in the child class) from showing up as a code parameter when instantiating an object of that derrived class then simply make it a member of the strict protected or strict private section.


With your example :


TLiveThing=class
strict protected
constructor Create(whereLive:string); virtual;
end;

THuman=class(TLiveThing)
public
constructor Create(whereLive:string); overload; override;
constructor Create(age:integer); overload;
end;

This will prevent the ancestor constructor Create(whereLive:string) from showing up as a parameter when you are creating an instance of your child class.


As pointed out by David, this has nothing to do with hiding the default Create constructor, it's only viable for hiding your custom constructors.


0 commentaires:

Enregistrer un commentaire