delphi 的虚方法和动态方法
delphi 的虚方法 和 动态方法有什么区别吗?
请大侠门说说
delphi虚方法和动态方法有什么用?
一般什么时候用啊?
谢谢大家
[解决办法]
功能上没区别。但虚方法注重调用速度的优化,动态方法注重代码大小的优化。所以一般经常需要后代码覆盖的方法用虚方法,不经常的用动态方法
[解决办法]
1楼的能讲清楚点吗,有相关的文章介绍吗?
[解决办法]
方法来到类中, 以前的特点基本都在;
因为类一般是存在于一个继承链中, 所以就有了一些新的概念, 譬如: 继承、覆盖;
也有了很多新名称, 譬如: 静态方法、虚方法、动态方法、抽象方法、类方法、消息方法.
先从虚方法与动态方法开始吧
--------------------------------------------
//下面的类中就定义了两个虚方法(virtual)、两个动态方法(dynamic)
TMyClass = class
procedure Proc1(x,y: Real); virtual;
function Fun1(x,y: Real): Real; virtual;
procedure Proc2(x,y: Real); dynamic;
function Fun2(x,y: Real): Real; dynamic;
end;
--------------------------------------------
//定义成虚方法或动态方法, 就意味着在后来的子类中将要被覆盖(override), 也就是重写
TBass = class
procedure Proc(x,y: Real); virtual;
function Fun(x,y: Real): Real; dynamic;
end;
TChild = class(TBass)
procedure Proc(x,y: Real); override;
function Fun(x,y: Real): Real; override;
end;
{正是因为这种机制而形成了多态}
--------------------------------------------
//那虚方法和动态方法有什么区别呢?
13.8px">
每个类都内含着两个表: 虚方法表(VMT)和动态方法表(DMT);
VMT 表包含着本类与其所有父类的虚方法 - 那一般会是一个比较庞大的表;
DMT 表只包含本类的动态方法 - 如果要调用其上层类的动态方法, 只能逐级查找;
因此, 使用虚方法速度上会有优势, 使用动态方法会节约内存;
在 Delphi 初期只有 virtual 而没有 dynamic ; 后来随着 VCL 日渐庞大, 才有了 dynamic ;
譬如类的事件方法一般都是在早期定义, 为了节约空间, 事件方法在 VCL 中基本都定义成了 dynamic ;
这样看来: virtual 和 dynamic 并没有太多区别, 一个侧重速度、一个节约空间; 它们是可以互相代替的!
另外: 因为它们区别不大, 并且是先有 virtual , 所以人们也习惯于把"虚方法"和"动态方法"都称作"虚方法".
以上来之万一的博客
[解决办法]
关于虚方法:比如DELPHI中经常用到一一种情况是当你的窗口用到继承的时候,子窗体继承父窗体的方法,此时你在父窗口声明一个
procedure Test(Sender: TObject);virtual;的事件,然后你的子窗体可以通过
procedure Test(Sender: TObject);override;对父窗体的Test事件进行重写。
procedure Test(Sender: TObject);override;
begin
inherited;//子窗体的重写代码中加上这个则表示仍继承父窗体的Test事件,删除则表示不继承。
end;
[解决办法]
[解决办法]
[解决办法]
虚方法表存在于每个派生类中.动态方法表只存在于实现该方法的类中.
虚方法调用是根据虚方法的偏移到表中直接调用.
动态方法则是在自己中找,没有就到父亲类中找,在没有就去爷爷那里找....
虚方法费空间但省时间.动态方法省空间但时间开销大.
如果是频繁调用的方法又不在乎一点点空间应该用虚方法.
如果是调用不多或者极端在意空间可以用动态方法.
不过对于触及使用者来说他们用法上没区别.
[解决办法]
RAD Studio
Virtual Methods
Expand AllVirtual methods employ a more complicated, and more flexible, dispatch mechanism than static methods. A virtual method can be redefined in descendant classes, but still be called in the ancestor class. The address of a virtual method isn't determined at compile time; instead, the object where the method is defined looks up the address at runtime.
To make a method virtual, add the directive virtual after the method declaration. The virtual directive creates an entry in the object's virtual method table, or VMT, which holds the addresses of all the virtual methods in an object type.
When you derive a new class from an existing one, the new class gets its own VMT, which includes all the entries from the ancestor's VMT plus any additional virtual methods declared in the new class.
-------------霸气的分割线-----------------
RAD Studio
Dynamic Methods
Dynamic methods are virtual methods with a slightly different dispatch mechanism. Because dynamic methods don't have entries in the object's virtual method table, they can reduce the amount of memory that objects consume. However, dispatching dynamic methods is somewhat slower than dispatching regular virtual methods. If a method is called frequently, or if its execution is time-critical, you should probably declare it as virtual rather than dynamic.
Objects must store the addresses of their dynamic methods. But instead of receiving entries in the virtual method table, dynamic methods are listed separately. The dynamic method list contains entries only for methods introduced or overridden by a particular class. (The virtual method table, in contrast, includes all of the object's virtual methods, both inherited and introduced.) Inherited dynamic methods are dispatched by searching each ancestor's dynamic method list, working backwards through the inheritance tree.
To make a method dynamic, add the directive dynamic after the method declaration.
[解决办法]
简单的理解是:为了子类重用或者覆盖,扩展性更强
比如100个窗体,都是继续某一个窗体,但100个窗体中都有一个方法代码是一样的
就可以把此方法写在父窗体中,声明为virtual;或者dynamic;那么这100个子窗体就可以不写一行代码了
有一天某个子窗体要做改动时,用override就可以覆盖此方法,在自己的单元里重写此方法,很方便
[解决办法]
李维的Inside VCL 深入核心VCL架构剖析
[解决办法]
http://topic.csdn.net/u/20120314/14/048cf4a7-76d3-4a60-9d35-96f6de05931a.html
看4L
[解决办法]
《面向对象编程思想》刘艺著
我觉得还不错,讲得比较清楚。