理解 Delphi 的类与接口(二) - 初识类的方法
2020-02-14 01:23
363 查看
说到"类", 就会提到:
属性、方法、事件 (这是类包含的内容);
封装、继承、多态 (这是类的主要用途).
下面定义并调用了了一个过程 MyProc、一个函数 MyFun.
把过程与函数 MyProc、MyFun 包含在一个类里面.
一般情况下, 类都会定义在 interface 区; 在 implementation 区定义的类只能本单元使用.
属性、方法、事件 (这是类包含的内容);
封装、继承、多态 (这是类的主要用途).
下面定义并调用了了一个过程 MyProc、一个函数 MyFun.
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//这是一个过程, 过程没有返回值
procedure MyProc(var x: Integer);
begin
x := x * 2;
end;
//这是一个函数, 函数必须有返回值
function MyFun(var x: Integer): Integer;
begin
x := x * 2;
Result := x;
end;
//调用过程与函数
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
i := 6;
MyProc(i); {使用过程}
ShowMessage(IntToStr(i)); {12}
i := 6;
i := MyFun(i); {使用函数}
ShowMessage(IntToStr(i)); {12}
end;
end.
把过程与函数 MyProc、MyFun 包含在一个类里面.
Unit1;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
TForm1 = (TForm)
Button1: TButton;
Button1Click(Sender: TObject);
;
Form1: TForm1;
TMyClass =
MyProc( x: Integer);
MyFun ( x: Integer): Integer;
;
TMyClassMyFun( x: Integer): Integer;
x := x * ;
Result := x; ;
TMyClassMyProc( x: Integer);
x := x * ; ;
TForm1Button1Click(Sender: TObject);
i: Integer;
myClass: TMyClass;
myClass := TMyClassCreate;
i := ;
myClassMyProc(i);
ShowMessage(IntToStr(i));
i := ;
i := myClassMyFun(i);
ShowMessage(IntToStr(i));
myClassFree; ;
.
一般情况下, 类都会定义在 interface 区; 在 implementation 区定义的类只能本单元使用.
unit
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
TForm1 = (TForm)
Button1: TButton;
Button1Click(Sender: TObject);
;
TMyClass =
MyProc( x: Integer);
MyFun ( x: Integer): Integer;
;
Form1: TForm1;
TMyClassMyFun( x: Integer): Integer;
x := x * ;
Result := x; ;
TMyClassMyProc( x: Integer);
x := x * ;
;
TForm1Button1Click(Sender: TObject);
i: Integer;
myClass: TMyClass;
myClass := TMyClassCreate; i := ;
myClassMyProc(i);
ShowMessage(IntToStr(i));
i := ;
i := myClassMyFun(i);
ShowMessage(IntToStr(i));
myClassFree; ;
.
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Delphi双指针使用
- 一些Delphi 控件下载地址备份 2011.12.15
- Delphi 数据库查询(TADOQuery)
- Delphi 内存分配 StrAlloc New
- Delphi 的编译指令
- delphi资源文件制作及使用详解
- 使用DelphiXE10动态创建Firebird嵌入式数据库文件
- Delphi 7连接MySql
- 用delphi解决MySQL数据库中无图形界面的问题
- DELPHI HOOK随手笔记
- DELPHI 读取EXCEL 以及将EXCEL写入到ACCESS 数据库
- delphi TByteArray 装换成base64
- Python for delphi教程
- 检查IP地址是否有效(delphi)
- delphi AddObject 用法
- delphi 动态创建 复选框控件
- delphi 通过点击获取控件名称
- Delphi中@,^,#,$分别表示什么?
- Delphi 运行时错误信息表
- Delphi 编译错误信息表