ugg 发表于 2006-10-23 11:29

delphi中的面向对象思想

unit Unit2;

interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
mammal=class    //哺乳动物
private
   ear:string; //耳朵
   eye:string;// 眼睛
   mouth:string; //嘴巴
   nose:string;//鼻子
public
procedure sound;virtual;abstract;
end;
//--------------------------------------------------------------------
type               //猫
cat=class(mammal)
private
public
procedure paw;virtual;abstract;
procedure sound;override;
end;

type
whitecat=class(cat)
private
public
procedure paw;override;
end;

type
blackcat=class(cat)
private
public
procedure paw;override;
end;
//------------------------------------------
type
dog=class(mammal)
private
public
procedure sound;override;
procedure watching;
end;

implementation

procedure cat.sound;
begin
showmessage('catsound');
end;
procedure whitecat.paw;
begin
showmessage('whitecatpaw');
end;
procedure blackcat.paw;
begin
showmessage('blackcatpaw');
end;
procedure dog.sound;
begin
showmessage('dogsound');
end;
procedure dog.watching;
begin
showmessage('dogwatching');
end;
end.

ugg 发表于 2006-10-23 11:30

unit Unit1;

interface

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

type
TForm1 = class(TForm)
   Button1: TButton;
   Button2: TButton;
   Button3: TButton;
   Button4: TButton;
   Button5: TButton;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
private
   { Private declarations }
public
   { Public declarations }
end;

var
Form1: TForm1;

implementation
uses unit2;

{$R *.dfm}
//procedure mymammal(my:mammal);
//begin
// my.sound;
//end;

procedure TForm1.Button1Click(Sender: TObject);
var
ocat:cat;
begin
ocat:=cat.Create;
ocat.sound;
ocat.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
ocat:whitecat;
begin
ocat:=whitecat.Create;
ocat.paw;
ocat.Free;
end;

end.

ugg 发表于 2006-10-23 11:34

实际上,DELPHI都可以实现C++的一些功能,很多人认为它是一种类似于VB的RAD思想的语言
其实不然,它完全可以实现OOP思想的,呵呵
页: [1]
查看完整版本: delphi中的面向对象思想