CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  语言基础/算法/系统设计

盼望大虾解释一个基础的问题:关于 class of ** 的问题

楼主searoom(海龙)2006-03-09 20:57:33 在 Delphi / 语言基础/算法/系统设计 提问

procedure   OpenMDIFrm(FrmClass:TFormClass;   var   frm);  
  begin  
      if   not   Assigned(TForm(frm))   then  
      begin  
          TForm(frm):=TForm(FrmClass.NewInstance);   //??  
          TForm(frm).Create(Application);                     //??   NewInstance   &   Create()   的区别  
      end;  
      TForm(frm).Show;  
  end;  
   
  问题点数:50、回复次数:6Top

1 楼searoom(海龙)回复于 2006-03-10 00:32:43 得分 0

Delphi   真的没落了?Top

2 楼mianbaosoft(不懂爱)(难道真的要回火星)回复于 2006-03-10 08:37:55 得分 5

class   of   定义的是类的类,可以同对象的类一样理解,主要看楼主想干吗Top

3 楼cncharles(旺仔)回复于 2006-03-10 08:50:18 得分 10

把下面的英文看明白就知道为什么这样.  
  Class-reference   types  
   
  A   class-reference   type,   sometimes   called   a   metaclass,   is   denoted   by   a   construction   of   the   form  
   
  class   of   type  
   
  where   type   is   any   class   type.   The   identifier   type   itself   denotes   a   value   whose   type   is   class   of   type.   If   type1   is   an   ancestor   of   type2,   then   class   of   type2   is   assignment-compatible   with   class   of   type1.   Thus  
   
  type   TClass   =   class   of   TObject;  
  var   AnyObj:   TClass;  
   
  declares   a   variable   called   AnyObj   that   can   hold   a   reference   to   any   class.   (The   definition   of   a   class-reference   type   cannot   occur   directly   in   a   variable   declaration   or   parameter   list.)   You   can   assign   the   value   nil   to   a   variable   of   any   class-reference   type.  
   
  To   see   how   class-reference   types   are   used,   look   at   the   declaration   of   the   constructor   for   TCollection   (in   the   Classes   unit):  
   
  type   TCollectionItemClass   =   class   of   TCollectionItem;  
      ...  
  constructor   Create(ItemClass:   TCollectionItemClass);  
   
  This   declaration   says   that   to   create   a   TCollection   instance   object,   you   must   pass   to   the   constructor   the   name   of   a   class   descending   from   TCollectionItem.  
   
  Class-reference   types   are   useful   when   you   want   to   invoke   a   class   method   or   virtual   constructor   on   a   class   or   object   whose   actual   type   is   unknown   at   compile   time.  
   
   
    NewInstance   method  
  Allocates   memory   for   an   instance   of   an   object   type   and   returns   a   pointer   to   that   new   instance.  
   
  Delphi   syntax:  
   
  class   function   NewInstance:   TObject;   virtual;  
   
  C++   syntax:  
   
  virtual   TObject*   __fastcall   NewInstance(TClass   cls);  
   
  Description  
   
  All   constructors   call   NewInstance   automatically.   NewInstance   calls   InstanceSize   to   determine   how   much   memory   to   allocate   from   the   heap   to   contain   a   particular   instance.   Do   not   call   NewInstance   directly.  
   
  Override   NewInstance   only   for   special   memory   allocation   requirements.   For   example,   when   allocating   a   large   number   of   identical   objects   that   all   need   to   be   in   memory   at   the   same   time,   you   could   allocate   a   single   block   of   memory   for   the   entire   group,   then   override   NewInstance   to   use   part   of   that   larger   block   for   each   instance.  
   
  If   you   override   NewInstance   to   allocate   memory,   you   may   need   to   override   FreeInstance   to   deallocate   the   memory.  
   
  Note: By   default   NewInstance   calls   InitInstance.  
   
  Top

4 楼GARNETT2183(KingWolves (http://kevin-lu.blogspot.com))回复于 2006-03-10 09:15:54 得分 15

类引用.也就是类的变量,可以类引用类型,变换不同的类...Top

5 楼wvins(逸岚)回复于 2006-03-10 10:26:35 得分 20

第一感觉和java中的class对象特别像  
  与c++中的typeinfo对象有些类似。  
   
  感觉和c++中的traits有些类似,traits是用来支持泛型编程的。  
  常见的应用就是通过class   of   xx的变量创建xx类型的变量。  
   
  var  
  x:   class   of   TObject  
   
  x.create就是创建一个TObject对象。  
  感觉java借鉴了delphi很多东西,包括在堆上产生对象。  
  java的class对象应该是从delphi中借鉴过去,完成同样的功能。  
   
  应该说能够实现一定的泛型编程,但目前刚入门还望高手指正  
  Top

6 楼wvins(逸岚)回复于 2006-03-10 10:29:25 得分 0

常见的应用就是通过class   of   xx的变量创建xx类型的变量。  
  //=============================================================  
  也就是楼上的一段E文中的那个例子!  
   
  只不过我非常讨厌delphi在例子中动不动就把TComponent搬出来  
   
  他的例子还可以更简单点!Top

相关问题

  • 基础问题,请帮我解释
  • 超级想不通:关于简单的消息问题!!急急,盼望高手解释
  • 请大家帮忙解释一下基础问题。
  • C基础问题求解(请给出详细解释)
  • 送分:帮忙解释一下(基础问题)
  • *** 连接池基础概念解释 (UP有分)
  • 基础问题,解释一下为什么是这个结果??
  • 【理论基础挑战】我解释不了的一段代码。
  • 学习中遇到的一个基础问题,麻烦达人给解释下。。。
  • 问一个基础问题,请高手深入解释一下,谢谢!

关键词

  • c++
  • delphi
  • 变量
  • frm
  • tform
  • 对象
  • 例子
  • reference
  • 创建
  • 类

得分解答快速导航

  • 帖主:searoom
  • mianbaosoft
  • cncharles
  • GARNETT2183
  • wvins

相关链接

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

广告也精彩

反馈

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