构造函数这块总是不对,帮忙看看(100分)

  • 主题发起人 主题发起人 koenemy
  • 开始时间 开始时间
K

koenemy

Unregistered / Unconfirmed
GUEST, unregistred user!
unit SearchBase;
interface
uses
IdHTTP;
type
TSearchBase = class
protected
keyword:String;
companyName:String;
function HttpRequestExecute(Url:string): string;
public
function GetPlace:String;virtual;abstract;
end;

implementation
function TSearchBase.HttpRequestExecute(Url: string): string;
var
Http: TIdHTTP;
begin
Http := TIdHTTP.Create(Nil);
try
Result := Http.Get(Url);
finally
Http.Free;
end;
end;
end.

--------------------------------------------------------------
unit baidu;
interface
uses
SearchBase;
Type
Tbaidu = class(TSearchBase)
public
function GetPlace:String;override;
Constructor Create(vkeyword,vcompanyName:String);
end;
implementation
function Tbaidu.GetPlace:String;
begin
Result := HttpRequestExecute('http://www.sina.com.cn');
end;
Constructor Tbaidu.Create(vkeyword,vcompanyName:String);
begin
keyword := vkeyword;
companyName := vcompanyName;
end;
end.
---------------------------------------------------------------------------

unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP,SearchBase,baidu, StdCtrls;

type
TForm1 = class(TForm)
mmo1: TMemo;
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
TS:TSearchBase;
TSon:Tbaidu;
begin
TS:=TSon.Create(edt1.Text,edt2.Text);
mmo1.Text := TS.GetPlace();
end;
end.
 
Constructor Tbaidu.Create(vkeyword,vcompanyName:String);
begin
inherited Create; //加这句
keyword := vkeyword;
companyName := vcompanyName;
end;
 
不行。。。基类没有构造函数,在这为什么要调用它呀
 
那报什么错啊! 起码有个错误描述吧??
 
project websiteplace.exe exception class eaccessviolation with message access violation at address 00403e9e in module websiteplace.exe read of address 000003a6 process stopped use step or run to continue
 
错误信息提示了什么
 
要加inherited,就算你没有指定,也是继承Tobject的。
 
小弟刚搞这delphi,

用try catch捕一下是不是。。。
 
Build
[Warning] main.pas(42): Variable 'TS' might not have been initialized
[Warning] main.pas(37): Variable 'TSon' might not have been initialized
就是这错误。。。。
我的代码全在这,老大们就看不出来有什么问题嘛。
 
TS:=TSon.Create(edt1.Text,edt2.Text); 这是错误的。
应该是TS:=TSearchbase.Create(edt1.Text,edt2.Text);
或者
TS:=TBaidu.Create(edt1.Text,edt2.Text);
因为你的TSearchBase类中有抽象方法, 所以最好用TBaidu来实例化。
 
[Warning] main.pas(42): Variable 'TS' might not have been initialized
[Warning] main.pas(37): Variable 'TSon' might not have been initialized
这是警告不是错误!!
 
procedure TForm1.btn1Click(Sender: TObject);
var
TS:TSearchBase;
begin
try
TS:=Tbaidu.Create(edt1.Text,edt2.Text);
except on e:exception do
begin
showmessage(e.Message)
end;
end;
mmo1.Text := TS.GetPlace();
TS.Free;
end;
这样捕异常 说ts不能被初始化,不能编译,,,,,,,,,,,delphi这玩意怎么这么费劲
 
procedure TForm1.btn1Click(Sender: TObject);
var
TS:TSearchBase;
//TSon:Tbaidu;
begin
TS:=Tbaidu.Create(edt1.Text,edt2.Text);
try
mmo1.Text := TS.GetPlace();
finally
TS.Free;
end;
end;
 
unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP,SearchBase,baidu, StdCtrls;

type
TForm1 = class(TForm)
mmo1: TMemo;
btn1: TButton;
edt1: TEdit;
edt2: TEdit;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
TS:TSearchBase;
begin
TS:=Tbaidu.Create(edt1.Text,edt2.Text);
try
mmo1.Text := TS.GetPlace();
finally
TS.Free;
end;
end;
end.

-----------------------------------------------------

unit SearchBase;
interface
uses
IdHTTP;
type
TSearchBase = class
protected
keyword:String;
companyName:String;
function HttpRequestExecute(Url:string): string;
public
function GetPlace:String;virtual;abstract;
end;

implementation
function TSearchBase.HttpRequestExecute(Url: string): string;
var
Http: TIdHTTP;
begin
Http := TIdHTTP.Create(Nil);
try
Result := Http.Get(Url);
finally
Http.Free;
end;
end;
end.

----------------------------------------------------------------


unit baidu;
interface
uses
SearchBase;
Type
Tbaidu = class(TSearchBase)
public
function GetPlace:String;override;
Constructor Create(vkeyword,vcompanyName:String);
end;
implementation
function Tbaidu.GetPlace:String;
begin
Result := HttpRequestExecute('http://www.sina.com.cn');
end;
Constructor Tbaidu.Create(vkeyword,vcompanyName:String);
begin
inherited Create;
keyword := vkeyword;
companyName := vcompanyName;
end;
end.
 
谢谢回复的兄弟
 
多人接受答案了。
 
都正确的呀
建议你查查运行的exe文件和你编译出来的文件是不是一个?
 
后退
顶部