• (5)第一个类的实例化


    unit Unit1;

    interface

    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
      System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

      // 父类
      TTest = class
      private // 不可见
        FValue: Integer;
      protected // 派生类可见
        procedure Add;
        procedure Dec;
        procedure Multiply;
        procedure Division;
        procedure show; virtual; // 虚方法
      public // 可见
        function GetFValue: Integer;
        constructor Create; // Constructor 构造函数,如果不写这个也不会出什么错,加上目的是为了在类被创建的时候执行一些初始化
        destructor Destroy; // Destructor  析构函数,这个也可以不写。加上的目的是为了在类销毁的时候进行一些资源的释放
      end;

      // 子类
      TMyTest = class(TTest)
      protected
        procedure show; override; // 覆盖
      end;

    var
      Form1: TForm1;
      MyTest: TMyTest; // 子类继承了父类的所有方法与属性,所以只需要定义子类就可以操作TTest的所有属性和方法了。

    implementation

    {$R *.dfm}
    { TTest }

    procedure TTest.Add;
    begin
      FValue := FValue + FValue; // 可直接使用类成员   相加
    end;

    constructor TTest.Create;
    begin
      FValue := 0; // 在类进行创建的时候把FValue设置为0
    end;

    procedure TTest.Dec;
    begin
      FValue := FValue - FValue; // 相减
    end;

    destructor TTest.Destroy;
    begin
      FValue := 0; // 在类进行释放的时候把FValue设置为0
    end;

    procedure TTest.Division;
    begin
      FValue := FValue div FValue; // 相除
    end;

    function TTest.GetFValue: Integer;
    begin
      Result := FValue; // 返回FValue的值
      ShowMessage(IntToStr(Result)); // 显示FValue的值
    end;

    procedure TTest.Multiply;
    begin
      FValue := FValue * FValue; // 相乘
    end;

    procedure TTest.show;
    begin
      ShowMessage('我是父类');
    end;

    { TMyTest }

    procedure TMyTest.show;
    begin
      inherited; // 继承父类的同名方法,在这里会执行父类的Show.
      ShowMessage('我是子类');
    end;

    { 实现过程 }
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MyTest.FValue := 10; // 赋值
      MyTest.Add; // 调用add方法、
      MyTest.GetFValue; // 返回结果,20
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      MyTest.FValue := 10; // 赋值
      MyTest.Dec; // 调用dec方法。
      MyTest.GetFValue; // 返回结果, 0
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
      MyTest.FValue := 10; // 赋值
      MyTest.Multiply; // 调用 Multiply方法
      MyTest.GetFValue; // 返回结果, 100
    end;

    procedure TForm1.Button4Click(Sender: TObject);
    begin
      MyTest.FValue := 10; // 赋值
      MyTest.Division; // 调用 Division方法
      MyTest.GetFValue; // 返回结果 1;
    end;

    procedure TForm1.Button5Click(Sender: TObject);
    begin
      MyTest.show; // 先显示'我是父类' ,再显示‘我是子类’
    end;

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      MyTest.Free; // 释放类,Free也是一个隐含的属性.
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      MyTest := TMyTest.Create; // 实例类
    end;

    end.

  • 相关阅读:
    FPGA开发全攻略——FPGA选型
    FPGA开发全攻略——FPGA开发基本流程
    希尔伯特变换的物理意义
    无线通信方式
    FPGA DDR3调试
    FPGA调试光纤模块
    FPGA FIFO深度计算
    Xilinx FPGA LVDS应用
    电源设计注意事项
    波特图与零极点
  • 原文地址:https://www.cnblogs.com/mdnx/p/2574636.html
Copyright © 2020-2023  润新知