inherited继承构造函数有关问题
inherited继承构造函数问题.
unit Unit1;
interface
uses
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
Thuman=class
private
name:String;
address:String;
age:Integer;
public
constructor Create(a,b:String;c:Integer);
end;
constructor Thuman.Create(a,b:String;c:Integer);
begin
name:=a;
address:=b;
age:=c;
form1.ListBox1.Items.Add('姓名为:'+a+','+'地址为:'+b+','+'年龄为'+inttostr(c));
end;
type
Tstudent=class(Thuman)
private
xinxi:String;
public
constructor Create(d:String);
end;
constructor Tstudent.Create(a,b,d:String;c:Integer);
begin
inherited Create(??????);//调用父类构造函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xinxi:=d;
form1.ListBox1.Items.Add('学校为:'+xinxi);
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
c1:Tstudent;
begin
c1:=Tstudent.Create();
end;
end.
代码里的问号处应该写几个参数啊,我写的基类里的3个构造函数,inherited Create(a,b,c);他提示我错啊..
------解决方案--------------------
type
Thuman = class
private
name: string;
address: string;
age: Integer;
public
constructor Create(a, b: string; c: Integer);
end;
constructor Thuman.Create(a, b: string; c: Integer);
begin
name := a;
address := b;
age := c;
if Assigned(form1) then
form1.ListBox1.Items.Add('姓名为: ' + a + ', ' + '地址为: ' + b + ', ' + '年龄为 ' + IntToStr(c));
end;
type
Tstudent = class(Thuman)
private
xinxi: string;
public
// constructor Create(d:String);
constructor Create(a, b, d: string; c: Integer);
end;
constructor Tstudent.Create(a, b, d: string; c: Integer);
begin
inherited Create(a, b, c); //调用父类构造函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xinxi := d;
if Assigned(form1) then
form1.ListBox1.Items.Add('学校为: ' + xinxi);
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
c1: Tstudent;
a, b, d: string;
c: Integer;
begin
a := 'A';
b := 'B';
d := 'D';
c:=32;
c1 := Tstudent.Create(a, b, d, c);
end;
unit Unit1;
interface
uses
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
Thuman=class
private
name:String;
address:String;
age:Integer;
public
constructor Create(a,b:String;c:Integer);
end;
constructor Thuman.Create(a,b:String;c:Integer);
begin
name:=a;
address:=b;
age:=c;
form1.ListBox1.Items.Add('姓名为:'+a+','+'地址为:'+b+','+'年龄为'+inttostr(c));
end;
type
Tstudent=class(Thuman)
private
xinxi:String;
public
constructor Create(d:String);
end;
constructor Tstudent.Create(a,b,d:String;c:Integer);
begin
inherited Create(??????);//调用父类构造函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xinxi:=d;
form1.ListBox1.Items.Add('学校为:'+xinxi);
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
c1:Tstudent;
begin
c1:=Tstudent.Create();
end;
end.
代码里的问号处应该写几个参数啊,我写的基类里的3个构造函数,inherited Create(a,b,c);他提示我错啊..
------解决方案--------------------
type
Thuman = class
private
name: string;
address: string;
age: Integer;
public
constructor Create(a, b: string; c: Integer);
end;
constructor Thuman.Create(a, b: string; c: Integer);
begin
name := a;
address := b;
age := c;
if Assigned(form1) then
form1.ListBox1.Items.Add('姓名为: ' + a + ', ' + '地址为: ' + b + ', ' + '年龄为 ' + IntToStr(c));
end;
type
Tstudent = class(Thuman)
private
xinxi: string;
public
// constructor Create(d:String);
constructor Create(a, b, d: string; c: Integer);
end;
constructor Tstudent.Create(a, b, d: string; c: Integer);
begin
inherited Create(a, b, c); //调用父类构造函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xinxi := d;
if Assigned(form1) then
form1.ListBox1.Items.Add('学校为: ' + xinxi);
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
c1: Tstudent;
a, b, d: string;
c: Integer;
begin
a := 'A';
b := 'B';
d := 'D';
c:=32;
c1 := Tstudent.Create(a, b, d, c);
end;