请高手帮忙---如何在多态下的一个子类中再写一个子类 ( 积分: 50 )

  • 主题发起人 主题发起人 晶凌
  • 开始时间 开始时间

晶凌

Unregistered / Unconfirmed
GUEST, unregistred user!
[gold]我写了一个交通工具的多态,其中有有飞机,船,汽车,火车,宇宙飞船,自行车,编写完成后我在FORM1中添加了飞机,船,汽车,火车,宇宙飞船,自行车六个按钮,运行正常,后来我想再在自行车这个子类中再添加红色自行车和蓝色自行车,又在FORM1上添加了红色自行车和蓝色自行车两个按钮,运行不过去,估计是写的红色自行车和蓝色自行车的类不对,找了半天编译器提示了不少错误,但改来改去还是没有改对,请各位高手相助,下面是没有添加红色自行车和蓝色自行车的多态程序,运行是正常的,烦劳各位高手帮忙填写一下在自行车这个子类下又添加的红色自行车和蓝色自行车子类的程序,谢谢
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;
Button6: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

TTransform=class
public
procedure ChooseTransform;virtual;abstract;
end;

TPlane=class(TTransform)
public
procedure ChooseTransform;override;
end;

TBoat=class(TTransform)
public
procedure ChooseTransform;override;
end;

TCar=class(TTransform)
public
procedure ChooseTransform;override;
end;

TTrain=Class(TTransform)
public
procedure ChooseTransform;override;
end;

TSpaceShip=class(TTransform)
public
procedure ChooseTransform;override;
end;

TBike=class(TTransform)
public
procedure ChooseTransform;override;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TPlane }
procedure TPlane.ChooseTransform;
begin
inherited;
//飞机
ShowMessage('您已经选择了飞机');
end;

{ TBoat }
procedure TBoat.ChooseTransform;
begin
inherited;
// 船
ShowMessage('您已经选择了船');
end;

{ TCar }
procedure TCar.ChooseTransform;
begin
inherited;
//汽车
ShowMessage('您已经选择了汽车');
end;

{ TTrain }
procedure TTrain.ChooseTransform;
begin
inherited;
//火车
ShowMessage('您已经选择了火车');
end;

{ TSpaceShip }
procedure TSpaceShip.ChooseTransform;
begin
inherited;
//宇宙飞船
ShowMessage('您已经选择了宇宙飞船');
end;

{ TBike }
procedure TBike.ChooseTransform;
begin
inherited;
//自行车
ShowMessage('您已经选择了自行车');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
a:TTransform;
begin
a:=TPlane.Create;
a.ChooseTransform;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
b:TTransform;
begin
b:=TCar.Create;
b.ChooseTransform;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
c:TTransform;
begin
c:=TTrain.Create;
c.ChooseTransform;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
d:TTransform;
begin
d:=TBoat.Create;
d.ChooseTransform;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
e:TTransform;
begin
e:=TSpaceShip.Create;
e.ChooseTransform;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
f:TTransform;
begin
f:=TBike.Create;
f.ChooseTransform;
end;

end.
[/gold]
 
你已经写的差不多了,接着继续~
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn4: TButton;
btn5: TButton;
btn6: TButton;
btn7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure btn7Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

TTransform=class
public
procedure ChooseTransform;
virtual;
abstract;
end;

TPlane=class(TTransform)
public
procedure ChooseTransform;override;
end;

TBoat=class(TTransform)
public
procedure ChooseTransform;override;
end;

TCar=class(TTransform)
public
procedure ChooseTransform;override;
end;

TTrain=Class(TTransform)
public
procedure ChooseTransform;override;
end;

TSpaceShip=class(TTransform)
public
procedure ChooseTransform;
override;
end;

TBike=class(TTransform)
public
procedure ChooseTransform;
override;
end;

TRedBike = class(TBike)
public
procedure ChooseTransform;
override;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TPlane }
procedure TPlane.ChooseTransform;
begin
inherited;
//飞机
ShowMessage('您已经选择了飞机');
end;

{ TBoat }
procedure TBoat.ChooseTransform;
begin
inherited;
// 船
ShowMessage('您已经选择了船');
end;

{ TCar }
procedure TCar.ChooseTransform;
begin
inherited;
//汽车
ShowMessage('您已经选择了汽车');
end;

{ TTrain }
procedure TTrain.ChooseTransform;
begin
inherited;
//火车
ShowMessage('您已经选择了火车');
end;

{ TSpaceShip }
procedure TSpaceShip.ChooseTransform;
begin
inherited;
//宇宙飞船
ShowMessage('您已经选择了宇宙飞船');
end;

{ TBike }
procedure TBike.ChooseTransform;
begin
inherited;
//自行车
ShowMessage('您已经选择了自行车');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
a:TTransform;
begin
a:=TPlane.Create;
a.ChooseTransform;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
b:TTransform;
begin
b:=TCar.Create;
b.ChooseTransform;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
c:TTransform;
begin
c:=TTrain.Create;
c.ChooseTransform;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
d:TTransform;
begin
d:=TBoat.Create;
d.ChooseTransform;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
e:TTransform;
begin
e:=TSpaceShip.Create;
e.ChooseTransform;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
f:TTransform;
begin
f:=TBike.Create;
f.ChooseTransform;
end;

{ TRedBike }
procedure TRedBike.ChooseTransform;
begin
inherited;
ShowMessage('您已经选择了自行车-Red');
end;

procedure TForm1.btn7Click(Sender: TObject);
var
f:TTransform;
begin
f:=TRedBike.Create;
f.ChooseTransform;
end;

end.
 
接受答案了.
 
后退
顶部