类的定义和使用

转自:http://mrmzy.bokee.com/tb.b?diaryId=15467469

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label5: TLabel;
    Edit5: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label6: TLabel;
    Label7: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;
type
    Tstudent=class
    private
      name:string;
      yy:real;
      math:real;
      hx:real;
      total:real;
    public
      procedure getname(nm:string);virtual;
      procedure getyy(cj:real);virtual;
      procedure getmt(cj:real);virtual;
      procedure gethx(cj:real);virtual;
      procedure caculat();virtual;
      function returntotal():real;virtual;
end;

implementation

function Tstudent.returntotal():real;
begin
returntotal:=total;
end;

procedure Tstudent.getname(nm:string);
begin
if name<>nm then
    name:=nm;
end;

procedure Tstudent.getyy(cj:real);
begin
if yy<>cj then
    yy:=cj;
end;

procedure Tstudent.getmt(cj:real);
begin
if math<>cj then
    math:=cj;
end;

procedure Tstudent.gethx(cj:real);
begin
if hx<>cj then
    hx:=cj;
end;

procedure Tstudent.caculat();
begin
total:=yy+math+hx;
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
student:Tstudent;
begin
if (edit1.Text=”) or (edit2.Text=”) or (edit3.Text=”) or (edit4.Text=”) then
     edit5.Text:=’请检查输入!’
else
     begin
       student:=Tstudent.Create;
       student.getname(edit1.Text);
       student.getyy(strtofloat(edit2.Text));
       student.getmt(strtofloat(edit3.Text));
       student.gethx(strtofloat(edit4.Text));
       student.caculat();
       edit5.Text:=floattostr(student.returntotal());
     end
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text:=”;
edit2.Text:=”;
edit3.Text:=”;
edit4.Text:=”;
edit5.Text:=”;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;

end.

Read: 23

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注