-----------------------------------------------------------------------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
type
Shape = class //定义父类.
public
color:String;
public
procedure Draw;
procedure Setcolor(c:String);
function Getcolor:String;
end;
procedure Shape.Draw; //图形借鉴
begin
Form1.ListBox1.Items.Add('画几何形状');
end;
procedure Shape.Setcolor(c:String);
begin
color:=c;
end;
function Shape.Getcolor:String;
begin
result:=color;
end;
type
Rectangle = class(Shape)
width:Integer;
height:Integer;
public
procedure Draw;
end;
procedure Rectangle.Draw;
var
mystr:String;
begin
mystr:='画一个宽为'+IntToStr(width)+',高为'+IntToStr(height)+'的'+color+'的矩形';
Form1.ListBox1.Items.Add(mystr); //
end;
type
Circle=class(Shape)
radius:Integer;
public
procedure Draw;
end;
procedure Circle.Draw;
var
mystr:String;
begin
mystr:='画一个半径为'+IntToStr(radius)+'的'+color+'的圆';
Form1.ListBox1.Items.Add(mystr);
end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
c1:Circle;
r1:Rectangle;
begin
c1:=Circle.Create; //!!!!!!!!!
r1:=Rectangle.Create;
c1.radius:=100;
c1.Setcolor('红色');
form1.ListBox1.Items.Add('圆的颜色为:'+c1.Getcolor);
r1.Draw;
r1.width:=100;
r1.height:=100;
r1.Setcolor('蓝色');
form1.ListBox1.Items.Add('矩形的颜色为:'+r1.Getcolor);
r1.Draw;
c1.Destroy;
r1.Destroy;
end;end.
--------------------------------------------------------------------------------------------------
一个比较初级的程序,有些问题一直不明白.程序里 c1:=Circle.Create; //!!!!!!!!!这句话的具体含义和Create的使用,还有 c1.Destroy;r1.Destroy;Destroy不是一般用于定义构析函数的名吗,为什么我没定义构析函数.也可以用?还有c1.Destroy;这句话在程序里的意思.?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
type
Shape = class //定义父类.
public
color:String;
public
procedure Draw;
procedure Setcolor(c:String);
function Getcolor:String;
end;
procedure Shape.Draw; //图形借鉴
begin
Form1.ListBox1.Items.Add('画几何形状');
end;
procedure Shape.Setcolor(c:String);
begin
color:=c;
end;
function Shape.Getcolor:String;
begin
result:=color;
end;
type
Rectangle = class(Shape)
width:Integer;
height:Integer;
public
procedure Draw;
end;
procedure Rectangle.Draw;
var
mystr:String;
begin
mystr:='画一个宽为'+IntToStr(width)+',高为'+IntToStr(height)+'的'+color+'的矩形';
Form1.ListBox1.Items.Add(mystr); //
end;
type
Circle=class(Shape)
radius:Integer;
public
procedure Draw;
end;
procedure Circle.Draw;
var
mystr:String;
begin
mystr:='画一个半径为'+IntToStr(radius)+'的'+color+'的圆';
Form1.ListBox1.Items.Add(mystr);
end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
c1:Circle;
r1:Rectangle;
begin
c1:=Circle.Create; //!!!!!!!!!
r1:=Rectangle.Create;
c1.radius:=100;
c1.Setcolor('红色');
form1.ListBox1.Items.Add('圆的颜色为:'+c1.Getcolor);
r1.Draw;
r1.width:=100;
r1.height:=100;
r1.Setcolor('蓝色');
form1.ListBox1.Items.Add('矩形的颜色为:'+r1.Getcolor);
r1.Draw;
c1.Destroy;
r1.Destroy;
end;end.
--------------------------------------------------------------------------------------------------
一个比较初级的程序,有些问题一直不明白.程序里 c1:=Circle.Create; //!!!!!!!!!这句话的具体含义和Create的使用,还有 c1.Destroy;r1.Destroy;Destroy不是一般用于定义构析函数的名吗,为什么我没定义构析函数.也可以用?还有c1.Destroy;这句话在程序里的意思.?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货