覆盖基类方法是不是基类方法必须定义为虚拟方法呢?(50分)

看看 delphi的帮助:
different parameter signature from its ancestor, it overloads the inherited method without hiding it. Calling the method in a descendant class activates whichever implementation matches the parameters in the call.
If you overload a virtual method, use the reintroduce directive when you redeclare it in descendant classes. For example,

type

T1 = class(TObject)
procedure Test(I: Integer)
overload
virtual;
end;
T2 = class(T1)
procedure Test(S: string)
reintroduce
overload;
end;
...
SomeObject := T2.Create;
SomeObject.Test('Hello!')
// calls T2.Test
SomeObject.Test(7)
// calls T1.Test

Within a class, you cannot publish multiple overloaded methods with the same name. Maintenance of runtime type information requires a unique name for each published member.

type

TSomeClass = class
published
function Func(P: Integer): Integer;
function Func(P: Boolean): Integer // error
 
To make a method virtual or dynamic, include the virtual or dynamic directive in its declaration. Virtual and dynamic methods, unlike static methods, can be overridden in descendant classes. When an overridden method is called, the actual (runtime) type of the class or object used in the method call梟ot the declared type of the variable梔etermines which implementation to activate.
To override a method, redeclare it with the override directive. An override declaration must match the ancestor declaration in the order and type of its parameters and in its result type (if any).

In the following example, the Draw method declared in TFigure is overridden in two descendant classes.

type

TFigure = class
procedure Draw
virtual;
end;
TRectangle = class(TFigure)
procedure Draw
override;
end;
TEllipse = class(TFigure)
procedure Draw
override;
end;

Given these declarations, the following code illustrates the effect of calling a virtual method through a variable whose actual type varies at runtime.

var

Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw
// calls TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
Figure.Draw
// calls TEllipse.Draw
Figure.Destroy;
end;
 
If a method declaration specifies the same method identifier and parameter signature as an inherited method, but doesn抰 include override, the new declaration merely hides the inherited one without overriding it. Both methods exist in the descendant class, where the method name is statically bound. For example,

type

T1 = class(TObject)
procedure Act
virtual;
end;
T2 = class(T1)
procedure Act
// Act is redeclared, but not overridden
end;

var

SomeObject: T1;
begin
SomeObject := T2.Create;
SomeObject.Act
// calls T1.Act
end;
 
You can declare more than one routine in the same scope with the same name. This is called overloading. Overloaded routines must be declared with the overload directive and must have distinguishing parameter lists. For example, consider the declarations

function Divide(X, Y: Real): Real
overload;

begin
Result := X/Y;
end;

function Divide(X, Y: Integer): Integer
overload;

begin
Result := X div Y;
end;
 
累死了。。。 。。。
楼主应该明白了吧
 
我没试过,我想不一定要定义为虚拟方法。。。

可以使用:inherited 。。来执行父类的方法。。。

例如你所说的:DoExit

procedure XXXXX.doExit(...)
begin
Inherited doExit;
end;
 
谢谢各位。其实多态的实现方面我是懂了。但我就是不明白为什么要这么样定义方法,这样定义方法有何好处。如:
TBasicF=class
procedure Show;
end;
TF=class(TBasicF)
procedure Show;
end;
{ TBasicF }

procedure TBasicF.Show;
begin
showmessage('basic show')
end;

{ TF }

procedure TF.Show;
begin
showmessage('TF show')
end;

var
F:TBasicF
//注意这儿:它为何不直接定义为TF,这样不就可以执行TF的方法了?;
begin
F:=TF.Create;
F.Show;
end;

这样做有什么样的优点勒,举个例说明,多谢各位啊!!
 
也就是说把对象定义为基类类型,然后用子类建立其实例,这种情况在OOP中要经常用到吗?还有,有哪些介绍这方面知识的书或实例? 请告诉我,谢谢!
MSN:softboy_hu@hotmail.com
 
有两类方法可以重载:虚方法(virtual)和动态方法(dynamic);
它们只是实现上略有不同。
DoExit是TWinControl的动态方法,可以重载
 
F:TBasicF
//注意这儿:它为何不直接定义为TF,这样不就可以执行TF的方法了?;
呵呵
如果有100个画各种图形的子类的话
那是不是要定义100个对象变量呢???????

但如果像F:TBasicF这样定义,你只需要定义一个变量
但确实可以完成所有的具体的画图操作,且指令都是draw
这就是多态的神奇之处啊:D

如果还没有领会,那我就无话可说啦
 
52free大虾,领会了。只是对基于oop的设计不知怎么搞才好。看来得多多来书了。
 
咦,刚看到!!!
讨论类的问题??
怎么跑出画图来啦~~
看上面该说的各位老兄都说了,
楼主还有什么问题吗????
呵呵~~~~~~~~~~~~~~~~~
 
多人接受答案了。
 
顶部