偶不明白:delphi的type中声明了:tform1:=class(form);button1:=tbutton;等,为什么却在最后又声明:var form1:=tform1,为什么不声明在t

chris2019 2002-04-13 11:27:07
tell me
...全文
338 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tf1008 2010-06-02
  • 打赏
  • 举报
回复
学习学习
kinggo 2002-04-13
  • 打赏
  • 举报
回复
为什么用
type
tform1=class(form)
button1=tbutton
而不用:
tform1=class(form)
tbutton1=class(button)
button1=tbutton
这样定义也可以
不过没有这个必要

dancemaple 2002-04-13
  • 打赏
  • 举报
回复
button1是一个对象(类实例),而tbutton是一个类
还是那句话:看看OOP的书吧
kinggo 2002-04-13
  • 打赏
  • 举报
回复
tform1:=class(form);button1:=tbutton这只不是定一个类
var form1:=tform1是定义一个类的变量也就是对象
chris2019 2002-04-13
  • 打赏
  • 举报
回复
为什么用
type
tform1=class(form)
button1=tbutton
而不用:
tform1=class(form)
tbutton1=class(button)
button1=tbutton
fermium 2002-04-13
  • 打赏
  • 举报
回复
同意楼上,另外var form1:Tform1 是戴毅一个实例,这些都是OOP的基础知识哈
erickleung 2002-04-13
  • 打赏
  • 举报
回复
可能你是惯用basic或c等编程, 随写随编.

Delphi沿用Pascal的体制, 任何物件都先要在定义区内定义,
然後在 implementation 执行区中使用.

正因为有这个规定, Pascal的好处便是较易跟进, 和其编译器
超级快.
liutaoboy 2002-04-13
  • 打赏
  • 举报
回复
TForm1是从Form中派生出来的一个类,而Form1是TForm1类型的变量,
这是两个概念,在Type中只是声明类,并没有把它实例化,建议你看一看
有关OOP或Java方面的书
dancemaple 2002-04-13
  • 打赏
  • 举报
回复
type是类型声明,var是变量(包括对象)定义
outer2000 2002-04-13
  • 打赏
  • 举报
回复
当然需要声明,前面派生了一个新的TFORM1;而FORM1又是。。
ihihonline 2002-04-13
  • 打赏
  • 举报
回复
tform1:=class(form);
var form1:=tform1
建议你看一看c++;
类-〉对象-〉事例;
BossHawk 2002-04-13
  • 打赏
  • 举报
回复
to liaokuo(辽阔)
写错了吧?这里没有实例化吗?
liaokuo 2002-04-13
  • 打赏
  • 举报
回复
不声明
var
form1 :tform;

也可以用,即使不构造事例,依然可以运行
application.create(Tform,Tform)
BossHawk 2002-04-13
  • 打赏
  • 举报
回复
可以这样 type Button1 = class(TButton) end;
这表示Button1是一个新的类(不是实例变量),接下来可以
var mybutton1: Button1;

为什么用
type
tform1=class(form)
button1=tbutton
而不用:
tform1=class(form)
tbutton1=class(button) //这里错了Form是一个类,Button不是,TButton是
button1=tbutton //也错了,用TButton1才是

为什么要TForm1而不TButton1呢?
因为你新建一个窗体时,不会(或很少)就一个空窗体吧?你得在里面放上其他东东(控件啦:)),这就是一个继承过程;但是,你会对一个TButton进一步加工吗?基本不会!(有时候是可以的!见后面的说明),所以你不必要用type tbutton1=class(Tbutton); var mybutton1: tbutton1;

特别说明:
tbutton有一个属性tag,大伙可以自由使用,那么你是不是觉得有时候不够用呢?比如你需要一个String型的属性而Hint又已经使用了?呵呵,可以这样:
type
TAdvButton = class(TButton)
private
yourstr: String;
end;

var
MyAdvButtonArr: array of TAdvButton;

然后就可以用了,区别是你要用代码创建新的TAdvButton,不能在设计时生成。

假如你想在设计时就生成,那么把这个TAdvButton作成控件安装上去就行。
怎么作呢?用Delphi的控件生成向导吧,太简单了,不过说起来烦,自己琢磨琢磨!



blazingfire 2002-04-13
  • 打赏
  • 举报
回复
To:chris2019(牛虻---最后的匈奴) :
Button1=TButton 这种用法没有,那么怎么用?
chris2019 2002-04-13
  • 打赏
  • 举报
回复
偶是说:
Button1=TButton
//这种用法好象没有

为什么不这样用呢?
blazingfire 2002-04-13
  • 打赏
  • 举报
回复
To:chris2019(牛虻---最后的匈奴)
---------------------
---------------------
TForm1=Class(TForm);
//这句话指是TForm1是从TForm继乘过来的一个子类
Button1=TButton
//这种用法好象没有
TButton1=Class(TButton)
//这句话也是指TButton1是从TButton中继乘过来的一个子类
var
Form1:TForm1;
//这是指定义了一个TForm1类的对象变量。
chris2019 2002-04-13
  • 打赏
  • 举报
回复
为什么用
type
tform1=class(form)
button1=tbutton
而不用:
tform1=class(form)
tbutton1=class(button)
button1=tbutton


delphi做的计算器。unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Scr: TLabel; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; Button10: TButton; Button11: TButton; Button12: TButton; Button13: TButton; Button14: TButton; Button15: TButton; Button16: TButton; Button17: TButton; Button18: TButton; Button19: TButton; Button20: TButton; procedure Button1Click(Sender: TObject); procedure Button12Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button11Click(Sender: TObject); procedure Button13Click(Sender: TObject); procedure Button20Click(Sender: TObject); procedure Button19Click(Sender: TObject); procedure Button18Click(Sender: TObject); private { Private declarations } pnt,er,fb:boolean; fstnm:Extended; fbcaption:string; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var temp:string; begin if er then exit; if pnt then scr.Caption:=scr.Caption+(sender as Tbutton).Caption else if (scr.Caption='0.') or fb then begin scr.Caption:=(sender as Tbutton).caption+'.'; fb:=false; end else begin temp:=scr.Caption; insert((sender as Tbutton).Caption,temp,length(temp)); scr.Caption:=temp; end; end; procedure TForm1.Button12Click(Sender: TObject); var temp:string; begin if er then exit; if scr.Caption<>'0.' then if pos('-',scr.Caption)>0 then begin temp:=scr.Caption; delete(temp,1,1); scr.Caption:=temp; end else scr.Caption:='-'+scr.Caption; end; procedure TForm1.FormCreate(Sender: TObject); begin pnt:=false; fstnm:=0; fb:=false; fbcaption:=''; er:=false; end; procedure TForm1.Button11Click(Sender: TObject); begin if er then exit; pnt:=true; end; procedure TForm1.Button13Click(Sender: TObject); begin if er then exit; if not fb then button20click(nil); if er then exit; fstnm:=strtofloat(scr.Caption); fbcaption:=(sender as Tbutton).Caption; fb:=true; pnt:=false; end; procedure TForm1.Button20Click(Sender: TObject); begin if er then exit; if fbcaption='+' then begin scr.Caption:=floattostr(strtofloat(scr.Caption)+fstnm); end; if fbcaption='-' then begin scr.Caption:=floattostr(fstnm-strtofloat(scr.Caption)); end; if fbcaption='*' then begin scr.Caption:=floattostr(strtofloat(scr.Caption)*fstnm); end; if fbcaption='/' then begin if scr.Caption<>'0.' then scr.Caption:=floattostr(fstnm/strtofloat(scr.Caption)) else begin scr.Caption:='zero divid error!'; er:=true; end; end; if pos('.',scr.caption)=0 then scr.Caption:=scr.Caption+'.'; fb:=true; pnt:=false; fbcaption:=''; end; procedure TForm1.Button19Click(Sender: TObject); begin pnt:=false; fstnm:=0; fb:=false; fbcaption:=''; er:=false; scr.Caption:='0.'; end; procedure TForm1.Button18Click(Sender: TObject); begin if er then exit; scr.Caption:='0.'; end; end.
DELPHI动态创建删除FRAME unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,FM; type TForm2 = class(TForm) Panel1: TPanel; Button2: TButton; ScrollBox1: TScrollBox; procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type TFM = Array Of TFrame1; var Form2: TForm2; aFM: TFM; procedure DeleteArrItem(var arr: TFM ; Index: Integer);stdcall; implementation {$R *.dfm} procedure DeleteArrItem(var arr: TFM ; Index: Integer); var Count: Cardinal; i:integer; begin Count := Length(arr); if (Count = 0) or (Index = Count) then Exit; Move(arr[Index+1], arr[Index], (Count-Index)* SizeOf(arr[0])); SetLength(arr, Count - 1); for I := 0 to Length(arr) - 1 do begin arr[i].Label1.Caption:=inttostr(i); end; end; procedure TForm2.Button2Click(Sender: TObject); var fram:TFrame1; begin SetLength(aFM,length(aFM)+1); aFM[length(aFM)-1] :=TFrame1.Create(nil) ; fram:=aFM[length(aFM)-1]; fram.Label1.Caption:=inttostr(length(aFM)-1); fram.Parent:=ScrollBox1; end; end. unit fm; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TFrame1 = class(TFrame) GroupBox1: TGroupBox; Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; implementation uses unit2; {$R *.dfm} procedure TFrame1.Button1Click(Sender: TObject); begin DeleteArrItem(aFM,strtoint(label1.Caption)); ( Sender as Tbutton ).Parent.Parent.Destroy; end; end.
开发来电显示的Delphi控件包: 使用方法: (1) 下载控件包Component.zip (2) 先编译 CPortLib6.dpk ,已安装了CPortLib 控件组的可省略这一步骤 (3) 然后编译 TelePort.dpk ,并安装 (4) 安装完成后,在 kosenTools 控件页增加了一个控件:TeleComport (5) 使用示例请看 Project2.dpr 示例源程序: unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, CPort, TeleComportUnit; type TForm1 = class(TForm) TeleComport1: TTeleComport; Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure TeleComport1Open(Sender: TObject); procedure TeleComport1Close(Sender: TObject); procedure TeleComport1Read(Sender: TObject; s: String; t: TDateTime); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button2Click(Sender: TObject); begin if TeleComport1.SetupPort then Memo1.Lines.Add(\'Port=\'+TeleComport1.Port); //设置来电管理器的连接端口,USB端口的设置为Port:=\"USB\" end; procedure TForm1.Button1Click(Sender: TObject); begin TeleComport1.Open; end; procedure TForm1.Button3Click(Sender: TObject); begin TeleComport1.Close; end; procedure TForm1.TeleComport1Open(Sender: TObject); begin if TeleComport1.JDConnected then begin Memo1.Lines.Add(\'Connected\'); //打开端口时读取来电管理器内缓存的号码 TeleComport1.ReadAllWhenOpen; end; end; procedure TForm1.TeleComport1Close(Sender: TObject); begin if not TeleComport1.JDConnected then Memo1.Lines.Add(\'not Connected\'); end; //有来电时触发此事件 //s为来电号码,t为来电时间 procedure TForm1.TeleComport1Read(Sender: TObject; s: String; t: TDateTime); begin Memo1.Lines.Add(s+F

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧