Delphi学习(三)——面向对象

1、新建单元,代码如下
unit Models;

interface
    uses Windows, SysUtils, Messages,Dialogs;

type
    Tstudent = class(TObject)    //创建类
    public
        sno : string;
        sname:string;
        Ssex : string;
        SAge : string;
        procedure SayHelloBy(name:string);
        function SayHelloByF(name:string): string;
    end;
implementation

procedure Tstudent.SayHelloBy(name:string);
begin
    showmessage('234'+sname);
end;

function Tstudent.SayHelloByF(name:string):string;
begin
    Result:=name;
end;
end.

窗体button Click事件如下
uses
    Models;

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
    student : Tstudent;
begin

实例化类
    student := Tstudent.Create;
    student.sname :='rxy';
    student.SayHelloBy('12');

    student.Free;//撤销类Free或Destroy

end;

原文地址:https://www.cnblogs.com/rongxiaoya/p/2787748.html