帮我看看,到底是什么问题 (100分)

  • 主题发起人 主题发起人 Cantin
  • 开始时间 开始时间
C

Cantin

Unregistered / Unconfirmed
GUEST, unregistred user!
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1}
Unit2 in 'Unit2.pas'


{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

unit Unit2;


interface

type
TSam=class
public
constructor Create;
function getvalue:string;
protected
value:string;
end;

TSam1=class(TSam)
public
constructor Create;
function getvalue:integer;
end;

implementation
uses
sysUtils;
{ TSam }

constructor TSam.Create;
begin
value:='shanghai'
{编译到这里老是出错?}
end;

function TSam.getvalue: string;
begin
getvalue:=value;
end;

{ TSam1 }

constructor TSam1.Create;
begin
value:='156';
end;

function TSam1.getvalue: integer;
begin
getvalue:=strtoint(value);
end;

end.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,unit2;

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
samfirst:TSam;
Samsecond:TSam1;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Samfirst.create;
SamSecond.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
Samfirst.Free;
SamSecond.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption :=Samfirst.getvalue;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption:=Inttostr(SamSecond.getvalue);
end;

end.

 
请将错误提示贴出
 
你的類要重載﹐你又沒有加上。
type
TSam=class
public
constructor Create;
function getvalue:string;virtual;
protected
value:string;
end;

TSam1=class(TSam)
public
constructor Create;
function getvalue:integer;overload;
end;
 
你最好换一个变量名
value是保留字
 
名字應該沒有問題的﹐你將錯誤代碼貼出來吧﹐不是Create的問題﹐我認為就是沒有重載的問題。
 
重載问题还有,变量名value是保留字
type
TSam=class
public
constructor Create;virtual;
function getvalue:string;virtual;
protected
str1:string;
end;

TSam1=class(TSam)
public
constructor Create;override;
function getvalue:integer;override;
end;

constructor TSam.Create;
begin
str1:='shanghai'
{编译到这里老是出错?}
end;

function TSam.getvalue: string;
begin
getvalue:=str1;
end;

{ TSam1 }

constructor TSam1.Create;
begin
str1:='156';
end;

function TSam1.getvalue: integer;
begin
getvalue:=strtoint(str1);
end;


 
>procedure TForm1.FormCreate(Sender: TObject);
>begin
> Samfirst.create;
> SamSecond.Create;
>end;
Samfirst := TSam.create;
SamSecond := TSam1.Create;



 
我写这段初级的代码主要是验证“重新定义静态对象的方法,只需在子类中重新定义该方法,参数可以和父类相同或不同”
谢谢大家对我的帮助!
分数给错了 应该是:dedman 真对不起
 
后退
顶部