CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  语言基础/算法/系统设计

一个类的方法被声明为dynamic和virtual有何用意,两者有何不同?

楼主jinkeb(猪哥亮)2003-08-03 09:45:47 在 Delphi / 语言基础/算法/系统设计 提问

一个类的方法被声明为dynamic和virtual有何用意,两者有何不同? 问题点数:50、回复次数:13Top

1 楼ljmanage(过客)回复于 2003-08-03 10:07:34 得分 10

dynamic就是它是动态方法,没有静态地址,所以速度慢一点  
  virtual是虚方法,如果在基类中不声明成虚方法的话,继承後,该方法会提示一警告信息,把基类中方法屏蔽掉了  
  Top

2 楼FrameSniper(http://naoku.net/blogs/framesniper/)回复于 2003-08-03 10:11:09 得分 10

什么名位动态(Dynamic)和虚拟(Virtual)后都可以使用Override关键字在派生类中对对应方法进行覆盖从而实现多态......  
   
  虚拟方法和动态方法在语义上可以说是等价的,他们唯一的区别在于对方法调用策略的内部实现上:虚拟方法从速度最优化的角度实现代码优化,而动态方法则是从空间最优的角度实现代码优化......Top

3 楼FrameSniper(http://naoku.net/blogs/framesniper/)回复于 2003-08-03 10:15:36 得分 0

 
  被覆盖的动态方法在派生类的VMT中不再出现........所以节省空间........但查询速度慢  
   
  被覆盖的虚拟方法在怕生类的VMT中再次出现........所以查询速度快......但浪费空间Top

4 楼shhgs()回复于 2003-08-03 14:04:48 得分 0

"Virtual   and   dynamic   methods   are   semantically   equivalent.   They   differ   only   in   the   implementation   of   method-call   dispatching   at   runtime.   Virtual   methods   optimize   for   speed,   while   dynamic   methods   optimize   for   code   size."  
   
  Virtual   和   dynamic   的方法在语义上是完全相同的。它们只是在运行时,方法调用的分派的实现方面有些不同。Virutal方法对速度进行了优化,而dynamic方法对代码的大小进行了优化。  
   
  ——摘自   Object   Reference   Language   Guide   第127页Top

5 楼xybh97102(冰点)回复于 2003-08-03 14:10:59 得分 0

OK,顶!Top

6 楼geyobing(大地精灵)回复于 2003-08-03 14:19:17 得分 0

又来晚了Top

7 楼lw549(那个孩子他爹)回复于 2003-08-04 11:22:03 得分 0

已阅Top

8 楼hkbarton(→Beginner←)回复于 2003-08-04 11:23:47 得分 0

学习学习,呵呵Top

9 楼rustle(刘敏(Rustle Liu))回复于 2003-08-06 13:49:25 得分 10

虚拟方法和动态方法  
     
   
  要使一个方法成为虚拟的或动态的,需要在其声明中包括指示字virtual或dynamic。虚拟方法和动态方法不同于静态方法,它们可以在其后裔类中被覆盖(overridden)。当一个覆盖方法被调用时,方法调用中使用的类或对象的实际(运行时)类型决定了哪一个实现是有效的,而非变量声明的类型决定。  
   
  要覆盖一个方法,需要在再声明中使用指示字override。一个override声明必需与其祖先声明在参数顺序和类型以及结果(如果有)等各方面匹配。  
   
  下面的例子中,TFigure中声明的Draw方法在两个后裔类中都被覆盖。  
   
  type  
   
      TFigure   =   class  
   
          procedure   Draw;   virtual;  
   
      end;  
   
      TRectangle   =   class(TFigure)  
   
          procedure   Draw;   override;  
   
      end;  
   
      TEllipse   =   class(TFigure)  
   
          procedure   Draw;   override;  
   
      end;  
   
  对于上面给出的声明,下面的代码举例说明了在运行时通过一个变量调用虚拟方法时,其类型变化的效果。  
   
  var  
   
      Figure:   TFigure;  
   
  begin  
   
      Figure   :=   TRectangle.Create;  
   
      Figure.Draw;     //调用的是TRectangle.Draw  
   
      Figure.Destroy;  
   
      Figure   :=   TEllipse.Create;  
   
      Figure.Draw;     //调用的是TEllipse.Draw  
   
      Figure.Destroy;  
   
  end;  
   
  只有虚拟方法和动态方法可以被覆盖。然而,所有的方法都可以被重载(overloaded),见重载方法。  
   
     
   
  虚拟和动态  
  虚拟方法和动态方法在语义上是等价的。其不同之处仅在于运行时对方法调用如何实现调度。  
  虚拟方法在速度方面相对更优化,而动态方法在代码尺寸方面相对更优化。  
   
  一般情况下,虚拟方法是实现多种行为最有效的途径。当一个基本类声明了许多可覆盖的方法,并且在应用程序中将被许多后裔类继承但很少覆盖时,动态方法是有用的。  
   
  联系作者  
   
  Email1:   cnsuyong@sina.com  
     
  Email2:   cnsuyong@hotmail.com  
     
  OICQ:   123010445  
     
  Top

10 楼rustle(刘敏(Rustle Liu))回复于 2003-08-06 13:51:15 得分 0

Virtual   and   dynamic   methods  
     
   
  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   not   the   declared   type   of   the   variable   determines   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;  
   
  Only   virtual   and   dynamic   methods   can   be   overridden.   All   methods,   however,   can   be   overloaded;   see   Overloading   methods.  
   
     
   
  Virtual   versus   dynamic  
  Virtual   and   dynamic   methods   are   semantically   equivalent.   They   differ   only   in   the   implementation   of   method-call   dispatching   at   runtime.   Virtual   methods   optimize   for   speed,   while   dynamic   methods   optimize   for   code   size.  
   
  In   general,   virtual   methods   are   the   most   efficient   way   to   implement   polymorphic   behavior.   Dynamic   methods   are   useful   when   a   base   class   declares   many   overridable   methods   which   are   inherited   by   many   descendant   classes   in   an   application,   but   only   occasionally   overridden.  
   
  Overriding   versus   hiding  
   
  Reintroduce  
   
  Abstract   methods  
   
     
   
  Topic   groups  
   
     
   
  See   also  
  Method   binging:   Overview  
   
  Static   methods  
   
  Top

11 楼Spacesoft(暗夜狂沙)回复于 2003-08-06 23:42:00 得分 0

我的《C++   和   Delphi   的函数覆盖(Override)与重载(overload)》里面说的比较详细。  
   
  http://www.csdn.net/develop/Read_Article.asp?Id=19672Top

12 楼wxjh(农民)回复于 2003-08-07 11:38:48 得分 10

注明:上面有同志误导大家  
  注意  
  Virtual的过程,函数都添加到   系统VMT中   //速度快  
  Dynamic的过程,函数添加到系统   DMT中     //空间少  
  都可以使用   OVERRIDE   (过载)Top

13 楼lw549(那个孩子他爹)回复于 2003-08-11 11:30:44 得分 10

被覆盖的动态方法在派生类的VMT中不再出现........所以节省空间........但查询速度慢  
   
  被覆盖的虚拟方法在怕生类的VMT中再次出现........所以查询速度快......但浪费空间  
   
  *********************  
   
  我觉得fs这段话已经说明一切了。详细情况参考《Delphi高手突破》(申min著)Top

相关问题

  • 接口的使用意义何在?
  • 这两个语句的用意何在?
  • 关于在函数前加::,用意何在。
  • PetShop3.0 用户控件类用的都是抽象类,用意何在?
  • formview背景如何用意一个BMP来渲染?(平铺效果)
  • update(true,false)不知道是什么语法,什么作用?意义何在!请教高手指点!!1
  • 这样书写,什么用意?
  • 讨论一下,virtual与dynamic的区别
  • 征询以下主板的使用意见,来者有分
  • 【【【100分问这个函数的作用意思】】】

关键词

  • 虚拟
  • 优化
  • virtual
  • 代码
  • 查询
  • 方法
  • tfigure
  • 调用
  • 覆盖
  • dynamic

得分解答快速导航

  • 帖主:jinkeb
  • ljmanage
  • FrameSniper
  • rustle
  • wxjh
  • lw549

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo