界面与逻辑分离(100分)

  • 主题发起人 主题发起人 七度时空
  • 开始时间 开始时间

七度时空

Unregistered / Unconfirmed
GUEST, unregistred user!
谈谈面向对象编程
 
{***工程文件***}
program pSayTwo;
uses
Forms,
ufrmSayTwo in 'ufrmSayTwo.pas' {Form1},
uSayDemo in 'uSayDemo.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
---------------------------------------------------------------
{***类单元***}
unit uSayDemo;
interface
type
IA=interface
['{8F0291F6-F91B-46BF-8D3E-BA431D624CC2}']
function SayHello:PChar;
end;
TMan=class(TInterfacedObject,IA)
public
Language:string;
Married:Boolean;
Name:string;
SkinColor:string;
constructor create;virtual;
function SayHello:PChar;virtual;abstract;
end;
TChinese=class(TMan)
public
constructor create;override;
private
function SayHello:PChar;override;
end;
TAmerican=class(TMan)
public
constructor create;override;
private
function SayHello:PChar;override;
end;
TFrench=class(TMan)
public
constructor create;override;
private
function SayHello:PChar;override;
end;
TKorean=class(TMan)
public
constructor create;override;
private
function SayHello:PChar;override;
end;
TET=class(TInterfacedObject,IA)
private
function SayHello:PChar;
end;
TRobot=class(TInterfacedObject,IA)
private
function SayHello:PChar;
end;
implementation
constructor TMan.create;
begin
Name:='金武';
Language:='中文';
SkinColor:='黄色 ';
end;
constructor TChinese.create;
begin
inherited;
end;
constructor TAmerican.create;
begin
Name:='bilee';
Language:='English';
SkinColor:='black';
end;
constructor TFrench.create;
begin
Name:='美丽莎';
Language:='法文';
SkinColor:='白色';
end;
constructor TKorean.create;
begin
Name:='朴顺锡';
Language:='韩文';
SkinColor:='黄色';
end;
function TChinese.SayHello;
begin
result:='chinese.bmp';
end;
function TAmerican.SayHello;
begin
Result:='American.bmp';
end;
function TFrench.SayHello;
begin
Result:='French.bmp';
end;
function TKorean.SayHello;
begin
Result:='Korean.bmp';
end;
function TET.SayHello;
begin
Result:='ET.bmp';
end;
function TRobot.SayHello;
begin
Result:='Robot.bmp';
end;
end.
---------------------------------------------------------------
{***用户界面***}
unit ufrmSayTwo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,uSayDemo;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
edtName: TLabeledEdit;
edtSkinColor: TLabeledEdit;
edtLanguage: TLabeledEdit;
btnUSA: TButton;
btnKroean: TButton;
btnCN: TButton;
btnFrench: TButton;
btnET: TButton;
btnRobot: TButton;
btnClose: TButton;
Image1: TImage;
procedure btnCloseClick(Sender: TObject);
procedure btnUSAClick(Sender: TObject);
procedure btnKroeanClick(Sender: TObject);
procedure btnCNClick(Sender: TObject);
procedure btnFrenchClick(Sender: TObject);
procedure btnETClick(Sender: TObject);
procedure btnRobotClick(Sender: TObject);
private
//注意这里的私有域,声明了一个类的重载方法和一个接口的重载方法
procedure SayHello(AMan:TMan);overload;
procedure SayHello(G:IA);overload;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//实现的方法是引用一个类TMan变量,通过重载overload
procedure TForm1.SayHello(AMan:TMan);
begin
edtName.Text:=AMan.Name;
edtSkinColor.Text:=AMan.SkinColor;
edtLanguage.Text:=AMan.Language;
image1.Picture.LoadFromFile(AMan.SayHello);
end;
//实现的方法是通过重载overload
procedure TForm1.SayHello(G:IA);
begin
edtName.Text:=copy(G.SayHello,1,6);
edtSkinColor.Text:=copy(G.SayHello,7,4);
edtLanguage.Text:=copy(G.SayHello,11,4);
Application.MessageBox(G.SayHello,'问候!',MB_ICONINFORMATION+MB_OK);
end;
procedure TForm1.btnUSAClick(Sender: TObject);
begin
SayHello(TAmerican.create);
end;
procedure TForm1.btnKroeanClick(Sender: TObject);
begin
SayHello(TChinese.create);
end;
procedure TForm1.btnCNClick(Sender: TObject);
begin
SayHello(TFrench.create);
end;
procedure TForm1.btnFrenchClick(Sender: TObject);
begin
SayHello(TKorean.create);
end;
procedure TForm1.btnETClick(Sender: TObject);
begin
SayHello(TET.create);
end;
procedure TForm1.btnRobotClick(Sender: TObject);
begin
SayHello(TRobot.create);
end;
procedure TForm1.btnCloseClick(Sender: TObject);
begin
Close;
end;
end.
 
接受答案了.
 
后退
顶部