delphi代码中类似下面的代码:
type
TMainForm = Class(TForm) //这行是什么意思?为什么不需要用";"结尾?
btnExit : TButton;
rgExitOptions : TRadioGroup;
procedure btnExitClick(sender : Tobject); //为什么要放这行?'sender : Tobject'是什么意思?
end;
除了上面两个问题,哪位有详细的关于delphi中'type'的用法等等?
type
TMainForm = Class(TForm) //这行是什么意思?为什么不需要用";"结尾?
btnExit : TButton;
rgExitOptions : TRadioGroup;
procedure btnExitClick(sender : Tobject); //为什么要放这行?'sender : Tobject'是什么意思?
end;
除了上面两个问题,哪位有详细的关于delphi中'type'的用法等等?
A type declaration specifies an identifier that denotes a type. The syntax for a type declaration istype newTypeName = typewhere newTypeName is a valid identifier. For example, given the type declarationtype TMyString = string;you can make the variable declarationvar S: TMyString;A type identifier's scope doesn't include the type declaration itself (except for pointer types). So you cannot, for example, define a record type that uses itself recursively.When you declare a type that is identical to an existing type, the compiler treats the new type identifier as an alias for the old one. Thus, given the declarationstype TValue = Real;
var
X: Real;
Y: TValue;X and Y are of the same type; at runtime, there is no way to distinguish TValue from Real. This is usually of little consequence, but if your purpose in defining a new type is to utilize runtime type information--for example, to associate a property editor with properties of a particular type--the distinction between "different name" and "different type" becomes important. In this case, use the syntaxtype newTypeName = type typeFor example,type TValue = type Real;forces the compiler to create a new, distinct type called TValue.For var parameters, types of formal and actual must be identical. For example, type
TMyType = type Integer
procedure p(var t:TMyType);
begin end;procedure x;
var
m: TMyType;
i: Integer;
begin
p(m); //Works
p(i); //Error! Types of formal and actual must be identical.
end;NoteThis only applies to var parameters, not to const or by-value parameters.
type中可以声明自定义的数据类型,包括记录、集合等Pascal中已有的类型,也包括Object中特有的类
在你的举例代码中就是声明了一个TMainForm的类,从TForm中继承而来,句末没有";"的原因是因为类的声明还没有完成,到最终的"End;"这个类的声明部分才算完成,请对比Record的声明方式:
type
TaRec = Record
F1: string;
F2: string;
end;procedure btnExitClick(sender : Tobject); 这个一行是声明了类的一个方法,方法就是属于类的过程或函数,其中的Sender是参数,TObject是它的类型,没有什么特殊的地方,就像你如果声明一个自己的方法,也可以写:
procedure DoSomething(Something: string);
这里的参数的Something,只不过类型是string。你的例子中的Sender类型是TObject;
这个方法是Delphi自动生成的事件出来方法,所以它有这样的参数名称和类型,至于为什么要传入这个Sender以及为什么它的类型是TObject,建议你先对Delphi作进一步的了解再来深究这个问题。
TMainForm = Class(TForm) //这行是什么意思?为什么不需要用";"结尾?
因为定义还没有结束,它是一个类定义
procedure btnExitClick(sender : Tobject); 这个一行是声明了类的一个方法
实际上,在具体实现时,sender 是具体发出消息激活btnExitClick的那个控件,例如,你的菜单中有N1、N4、N8、N10都要调用btnExitClick方法,在btnExitClick如何区分是谁呢,这就要用到sender 了:
在处理中可以这样:
if sender=N1 then {执行N1规定的处理}
if sender=N4 then {执行N4规定的处理}
if sender=N8 then {执行N8规定的处理}
if sender=N10 then {执行N10规定的处理}