汽车类继承的问题

类继承的问题
我举个例子,定义一个“车”类(非结口),汽车,货车 都是“车”类的子类。
我现在有20辆车,其中10改造成汽车,10改造成货车。这样的继承关系怎样实现??
车=CLASS
public
 车轮:TObject;
 procedure Move;
 procedure Stop;
 procedure AddOil;
 procedure Create;
end;
汽车=CLASS(车)
public
 汽油箱:TObject
 procedure TakeManyPeople;
end;
货车=Class(车)
public
 货箱:TObject
 procedure TakeManythings;
end;
Procedure 车.Create()
begin
车轮.Create();
end;
Procedure 汽车.Create()
begin
汽油箱.Create();
end;
Procedure 汽车.Create()
begin
货箱.Create();
end;
汽车1:=汽车(车1);
汽车1.汽油箱 (这个对象就不存在)

想了几天只想到一个办法
Create 汽车;
汽车.Assign(车);
车.Delete;
请教一下各位高手有没有什么更好的办法??

来自: SnakeArl, 时间: 2005-10-27 10:32:01, ID: 3245530

子类可以继承父类的所有特性,还可以指定其自己本身的特有属性吧!
我是新手,理解也不太透彻,呵呵``

来自: mmzmagic, 时间: 2005-10-27 10:35:32, ID: 3245536

车=CLASS
public
 procedure Move;
 procedure Stop;
 procedure AddOil;
end;
 
汽车=CLASS(车)
public
 procedure TakeManyPeople;
end;
货车=Class(车)
public
 procedure TakeManythings;
end;

=============
var
 i:integer;
for i:=0 to 9 do  begin
 汽车.Create;
 货车.Create;
end;

来自: 总有爱, 时间: 2005-10-27 10:36:27, ID: 3245539

车怎么能改造成汽车呢?他具备汽车足够的属性吗?同样具备货车足够的属性吗?

来自: mmzmagic, 时间: 2005-10-27 10:37:38, ID: 3245542

子类可以转换成为父类
因为子类里边包括父类的所有成员
但是父类却不能转换为子类
所以你的想法不对 不是一开始就创建20辆车 而应该分别创建10辆汽车和10辆货车

来自: wwr74, 时间: 2005-10-27 11:13:31, ID: 3245615

汽油箱是汽车的。车是没有这个属性的。当然无法得到。
可以车(汽车),反过来可不行

来自: whqsoft, 时间: 2005-10-27 11:18:16, ID: 3245632

如果一开始就建10辆汽车和10辆货车  我就不用在转换了

来自: wrl_001, 时间: 2005-10-27 11:32:08, ID: 3245668

看这样代码如何:[:D]
type
 TVehicle=class
 public
   Field1:Integer;
   Field2:Integer;
   constructor Create;
 end;
 TCar=class(TVehicle)
 public
   Field3:Integer;
   constructor Create(ReBuild:TVehicle);virtual;
 end;
 TTruck=class(TVehicle)
 end;
 TForm1 = class(TForm)
   Button1: TButton;
   Label1: TLabel;
   Label2: TLabel;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

{ TCar }

constructor TCar.Create(ReBuild: TVehicle);
begin
 Field1:=Rebuild.Field1;
 if Rebuild.Field2=2 then
   Field2:=20  //进行改造
 else Field2:=25;
 Field3:=30;
 ReBuild.Free;
end;

{ TVehicle }

constructor TVehicle.Create;
begin
 Field1:=1;
 Field2:=2;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 ves:array[0..9] of TVehicle;
begin
 ves[0]:=TVehicle.Create;
 ves[0]:=TCar.Create(ves[0]);
 ves[1]:=TVehicle.Create;
 ves[1].Field2 :=3;
 ves[1]:=TCar.Create(ves[1]);
 label1.Caption :=inttostr(ves[0].Field2);
 label2.Caption :=inttostr(ves[1].Field2);
 ves[0].Free;
 ves[1].Free;
end;

来自: bluesaga, 时间: 2005-10-27 12:55:30, ID: 3245765

你是想达到什么目的?

来自: whqsoft, 时间: 2005-10-27 16:18:56, ID: 3246108

楼上的英语太好了,这种办法不错,
constructor TCar.Create(ReBuild: TVehicle);
 Field1:=Rebuild.Field1;
 ReBuild.Free;
最后还是要把ReBuild.free,实际上制造了40车辆。
thank 大家!!!!! 我的目的是:
我写了一个程序,程序里的类都是从xmlNode继承过来的,我用xmldoc.loadfromfile();
以后,这些结点都已创建出来了(也就是20辆车有了),但是我要后面我要把它变成
汽车,汽车类
看了大家给我这么多帮助,我觉得楼上写的方法不错,我决定这样写:
汽车1=汽车.create();
汽车1.assign(xmlnode1);
汽车1.Replace(xmlNode1);
xmlNode1.free;

来自: wwa, 时间: 2005-11-13 20:46:44, ID: 3264364

不要用继承,用组合
汽车=class
 v1: 车;
 汽油箱:TObject
 procedure TakeManyPeople;
end;

问题讨论没有结束 ...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值