我希望了解
当应用于对象构造函数时.每次我随机添加关键字直到编译器关闭 - 并且(在使用Delphi开发12年之后)我宁愿知道我在做什么,而不是随意尝试.
给出一组假设的对象:
TComputer = class(TObject)
public
constructor Create(Cup: Integer); virtual;
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual;
end;
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); override;
constructor Create(Cup: Integer; Teapot: string); override;
end;
Run Code Online (Sandbox Code Playgroud)
我希望它们表现的方式可能从声明中可以明显看出,但是:
TComputer 有简单的构造函数,后代可以覆盖它TCellPhone 有一个替代构造函数,后代可以覆盖它TiPhone 覆盖两个构造函数,调用每个构造函数的继承版本现在该代码无法编译.我想明白为什么它不起作用.我也想了解覆盖构造函数的正确方法.或许你永远不能覆盖构造函数?或者覆盖构造函数是完全可以接受的?也许你永远不应该有多个构造函数,也许完全可以接受多个构造函数.
我想了解原因.修复它会很明显.
编辑:我也期待获得的订单上的推理virtual,override,overload,reintroduce.因为在尝试关键字的所有组合时,组合的数量会爆炸:
override和reintroduce指令有什么区别?什么时候不应该inherited在重写方法中使用关键字?
当我忘记在最近添加关键词'virtual'和'override'时,我会在意外地为派生类中的过程使用相同的名称时发出编译器警告.我没有,现在我不明白为什么.我需要做些什么来获取隐藏基本成员和方法的警告?
根据这个答案(Jim McKeeth,毫无疑问是正确的):
如果在后代类中声明一个与祖先类中的方法同名的方法,那么您将隐藏该祖先方法 - 这意味着如果您有该后代类的实例(被引用为该类)那么您将没有得到祖先的行为.编译器会给你一个警告.
但是,令我惊讶的是这段代码没有给我一个警告:
unit Unit1;
interface
{$WARNINGS ON}
{$WARN HIDING_MEMBER ON}
{$WARN HIDDEN_VIRTUAL ON}
// I understand the two lines above are superfluous.
// I put them there to demonstrate that I have tried to enable these
// warnings explicitly.
type
TBase = class
public
SomeMember: integer;
procedure Foo;
end;
type
TDerived = class (TBase)
public
SomeMember: integer;
procedure Foo;
end;
implementation
{ TBase }
procedure TBase.Foo;
begin
end;
{ TDerived }
procedure TDerived.Foo; …Run Code Online (Sandbox Code Playgroud) 我创建了一个类
FormInfo = class (TComponent)
private
FLeftValue : Integer;
FTopValue : Integer;
FHeightValue : Integer;
FWidthValue : Integer;
public
constructor Create(
AOwner : TComponent;
leftvalue : integer;
topvalue : integer;
heightvalue : integer;
widthvalue : integer);
protected
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
//procedure SetParentComponent(Value : TComponent); override;
published
property LeftValue : Integer read FLeftValue write FLeftValue;
property TopValue : Integer read FTopValue write FTopValue;
property HeightValue : Integer read FHeightValue write FHeightValue;
property WidthValue : …Run Code Online (Sandbox Code Playgroud) delphi ×4
constructor ×2
build ×1
delphi-5 ×1
delphi-xe2 ×1
overriding ×1
polymorphism ×1
warnings ×1