DELPHI8操作符重载的例子

unit WinForm;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data;

type
  TWinForm = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    /// <summary>
    /// Required designer variable.
    /// </summary>
    Components: System.ComponentModel.Container;
    Button1: System.Windows.Forms.Button;
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    procedure InitializeComponent;
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
  {$ENDREGION}
  strict protected
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;

  //写成类也可以,这里我用了记录。由于记录是值类型省去了创建实例的麻烦
  TClassTest=record
  public
    FA:Integer;
    //重载了“+”操作符
    class operator add(A,B:TClassTest):TClassTest;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

implementation

{$REGION 'Windows Form Designer generated code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWinForm.InitializeComponent;
begin
  Self.Button1 := System.Windows.Forms.Button.Create;
  Self.SuspendLayout;
  //
  // Button1
  //
  Self.Button1.Location := System.Drawing.Point.Create(96, 88);
  Self.Button1.Name := 'Button1';
  Self.Button1.Size := System.Drawing.Size.Create(392, 112);
  Self.Button1.TabIndex := 0;
  Self.Button1.Text := 'Button1';
  Include(Self.Button1.Click, Self.Button1_Click);
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(6, 14);
  Self.ClientSize := System.Drawing.Size.Create(560, 357);
  Self.Controls.Add(Self.Button1);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;

procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
var
  A,B,C:TClassTest;
begin
  A.FA :=1;
  B.FA :=2;
  C:=A+B; //两个结构(或者类)用+操作,在DELHI8以前是不可想象的
  system.Windows.Forms.MessageBox.Show(System.Convert.ToString(C.FA) );
end;

{ TClassTest }

//重载“+”操作符的实现
class operator TClassTest.add(A, B: TClassTest): TClassTest;
begin
  Result.FA:=A.FA + B.FA;
end;

end.

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Delphi是一种编程语言,提供了丰富的库来开发各类应用程序。其中,UDP协议是一种无连接的传输协议,适用于一对一或一对多的通信。 Delphi提供了UDP服务器的例子,可以帮助开发者快速搭建UDP服务器应用程序。使用Delphi编写UDP服务器需要使用TIdUDPServer组件。 以下是一个简单的Delphi UDP服务器的例子: 1. 首先,在Delphi中创建一个新的控制台应用程序项目。 2. 双击项目窗口中的"Form1",在Form1的代码编辑器中添加一个TIdUDPServer组件。代码如下: ```delphi unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdUDPServer, Vcl.StdCtrls; type TForm1 = class(TForm) IdUDPServer1: TIdUDPServer; Memo1: TMemo; procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TBytes; ABinding: TIdSocketHandle); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin IdUDPServer1.DefaultPort := 1234; IdUDPServer1.Active := True; end; procedure TForm1.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; AData: TBytes; ABinding: TIdSocketHandle); var ReceivedText: string; begin ReceivedText := StringOf(AData); Memo1.Lines.Add('Received: ' + ReceivedText); end; end. ``` 3. 添加一个TMemo控件,用于显示从客户端接收到的数据。 4. 在Form1的OnCreate事件中启动UDP服务器,设置默认端口为1234。 5. 添加UDP数据接收事件,当接收到客户端发送的数据时,将数据显示在Memo控件中。 可以使用Delphi设计界面并对代码进行编译和运行。通过UDP客户端向服务器发送数据,服务器将在Memo控件中显示接收到的数据。 这是一个简单的Delphi UDP服务器的例子,仅供参考。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武稀松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值