关于类请教(100分)

  • 主题发起人 主题发起人 2843223
  • 开始时间 开始时间
2

2843223

Unregistered / Unconfirmed
GUEST, unregistred user!
自己写了一个测试类<br>&nbsp;TCardMachine=class(TInterfacedObject,IMachine)<br>&nbsp; Public<br>&nbsp; &nbsp; constructor Create;<br>&nbsp; &nbsp; Destructor &nbsp;Destroy;<br>&nbsp; &nbsp; Procedure Free; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; virtual;abstract;<br>&nbsp; &nbsp; Function Read(var Rbuff:TDataBuff):Boolean ; virtual;abstract;<br>在子类中<br>TCardM1 &nbsp; &nbsp;=class(TCardMachine)<br>&nbsp; private<br>&nbsp; &nbsp; Function Connect :boolean;override;<br>&nbsp; &nbsp; procedure DisConnect; override;<br>&nbsp; Public<br>&nbsp; &nbsp; Constructor Create; OverLoad;<br>&nbsp; &nbsp; Destructor &nbsp;Destroy;overload;<br>&nbsp; &nbsp; Procedure Free ; override;<br>&nbsp; &nbsp; Function Read (var Rbuff:TDataBuff;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Password:string='ffffffffffff'):Boolean ; &nbsp;overload;<br><br>我在调用的过程中这么使用<br>var m1 :TCardMachine;<br>m1:=tcardm1.create;<br>m1.read 时 为什么出错 ,我知道 是因为TCardMachine.read是虚拟的,但是我用的是<br>子类 tcardm1创建的呀。<br>如果解决,请问怎么解决
 
看你输入Rea函数的的参数,注意到子类的参数是2个而父类是1个,假如你是1个参数调用,系统会认为调用父类的Read函数。
 
参数已经输入了read(buff,'aaa').<br>系统提示参数不正确
 
子类<br>Function Read (var Rbuff:TDataBuff;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Password:string='ffffffffffff'):Boolean ; &nbsp;overload;<br>改为<br>Function Read (var Rbuff:TDataBuff;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Password:string='ffffffffffff'):Boolean ; &nbsp;override;<br><br><br>父类改为<br>Function Read (var Rbuff:TDataBuff;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Password:string='ffffffffffff'):Boolean ; &nbsp;virtual; abstract;<br><br><br>另外不要重载Free
 
不行,,就是因为<br>&nbsp; Function Read(var Rbuff:TDataBuff):Boolean<br><br>Function Read (var Rbuff:TDataBuff;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Password:string='ffffffffffff'):Boolean ;override;<br>是不同的。。。
 
不同的就不能这样调用<br>因为你声明的变量类型是TCardMachine<br>而TCardMachine的Read方法只有一个参数,它只认父类TCardMachine.Read这个方法<br>要么用TCardM1(m1).Read调用子类的方法,要么子类override父类的虚方法<br><br>还有,你的TCardMachine类的Read方法是纯虚方法,子类如果要实例化的话,应该实现这个方法
 
overload改override
 
改了父类其他的都得改,没有更好的方法么??
 
后退
顶部