初学请教简单问题(30分)

N

nong

Unregistered / Unconfirmed
GUEST, unregistred user!
我要写的是个复数类。
unit shu;
interface
type
Tshu = class
public
shibu,xubu:integer;//实部,虚部
public
constructor Create(a,b:integer);
function add(i:tshu):tshu;//复数加法
function sum:integer;
end;
implementation
uses
Main;
constructor Tshu.Create(a,b:integer);
begin
shibu:=a;
xubu:=b;
end;
function tshu.sum:integer;
begin
result:=shibu+xubu;//可以直接用
end;
function tshu.add(i:tshu):tshu;
begin
result.shibu:=?。shibu+i.shibu;//但是这里不能直接用,感觉要有?这个东东
result.xubu:=?。xubu+i.xubu;//this,it,self都不是,是什么?
end;
//或者有更好的写复数这个类的更好想法,帮帮忙。
//刚学delphi又没有书参考,只有上网学习,拜谢各位。
end.
 
看看书,把如何定义,使用对象的属性搞清楚,
DELPHI的VCL源码是最好的教材
 
tshu是自定义类
function tshu.add(i:tshu):tshu;//类相加好用吗????没听说过………

 
哪位高手直接告诉我答案不就行了,我没有书啊。谢谢谢谢
 
各位救命啊,没人帮忙吗
 
function tshu.add(i:tshu):tshu;
begin
result.shibu:=self.shibu+i.shibu;
//就是self!!!,因为对象方法都隐含了一个self指针,来指向调用这个方法的对象
result.xubu:=self.xubu+i.xubu;
end;
可能是其他地方出问题了。
请注意对齐,不然大家看起来很费劲
 
晕,怎么可以写两个public
public
shibu,xubu:integer;//实部,虚部
public

 
你最好能把出错信息贴出来。
 
另外,F1就是最好的书,VCL也是书。何况还有那么多的电子书!!
学东西要积极点
 
多谢指点,我我会的,先试试
 
如果用self,出错是:
project fushu.exe raised exception class EAccessViolation with
message 'access violation at address 004500ef in module
'fushu.exe'.read of address FFFFFFFF' .process stopped use step or run
to continue.
 

function tshu.add(i:tshu):tshu;
begin
Result:=Tshu.Create(0,0);
result.shibu:=self.shibu+i.shibu;
//就是self!!!,或者不写!!你的出错提示是内存访问出错!!
//是因为Result也是Tshu对象,它没有没有创建!但你使用了它的数据成员。so..
result.xubu:=self.xubu+i.xubu;//
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i,j:Tshu;
begin
i:=Tshu.create(10,5);
j:=Tshu.create(12,6);
showmessage(format('%d+%di',[j.add(i).shibu,j.add(i).xubu]));
end;
 
to:majorsoft
真是太感谢了,我看了很多书上面关于这个只是略提,有很多问题看书很难解决,实际上我是很认真的,可是旁边没人指点。
你真行,我的QQ10364504交个朋友吧。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
664
import
I
I
回复
0
查看
616
import
I
顶部