再问:实现接口,返回子类 [英] Asking again: Implementing interface, returning subclass

查看:68
本文介绍了再问:实现接口,返回子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;


我正在编写一个实现IDbConnection的类。 i / f定义了一个方法

BeginTransaction()返回一个IDbTransaction。我想将此定义为

返回DbTransaction(如DbConnection所做)。这对我来说很有意义

这是合法的,因为DbTransaction实现了IDbTransaction,所以它确实符合返回IDbTransaction的

合约。


但它不会编译 - 我如何让它工作?必须有一种方式,因为

DbConnection这样做。


我尝试过显式/隐式但是收到一条错误消息说只能是

与运营商一起使用。


-

谢谢 - 戴夫

david_at_windward_dot_net
http://www.windwardreports.com

Hi;

I am writing a class that implements IDbConnection. The i/f defines a method
BeginTransaction() that returns an IDbTransaction. I want to define this as
returning a DbTransaction (as DbConnection does). It makes sense to me that
this is legal as DbTransaction implements IDbTransaction so it does meet the
contract of returning an IDbTransaction.

But it won''t compile - how do I get this to work? There must be a way as
DbConnection does it.

I tried explicit/implicit but got an error message saying that could only be
used with operators.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

推荐答案

>我认为
这是合法的,因为DbTransaction实现了IDbTransaction,所以它确实符合
返回IDbTransaction的合同。
不。接口变量不知道在接口类的外部声明的方法 - 例如在派生类中。所以我似乎(并且我没有看到你的代码)你试图访问一个方法

,其签名不在适当的接口类中。


-

温馨的问候,

Alvin Bruney [MVP ASP.NET]


[无耻的作者插件]

带有.NET的Microsoft Office Web Components黑皮书

现在可用@ a a rel =nofollowhref =http: //www.lulu.com/owctarget =_ blank> www.lulu.com/owc

专业版VSTO 2005 - Wrox / Wiley 2006

博客: http://msmvps.com/blogs/Alvin/

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


" David Thielen" <第***** @ nospam.nospam>在留言中写道

新闻:BC ********************************** @ microsof t.com ...嗨;

我正在编写一个实现IDbConnection的类。 i / f定义了
方法BeginTransaction(),它返回一个IDbTransaction。我想将此
定义为返回DbTransaction(如DbConnection所做)。这对我来说是合理的,因为DbTransaction实现了IDbTransaction,这是合法的,所以它确实符合
返回IDbTransaction的合同。

但它不会编译 - 怎么做我让这个工作?必须有一种方式,因为DbConnection会这样做。

我尝试了显式/隐式,但得到一条错误消息,说只有
可以与运营商一起使用。

-
谢谢 - 戴夫
david_at_windward_dot_net
http:/ /www.windwardreports.com
this is legal as DbTransaction implements IDbTransaction so it does meet the contract of returning an IDbTransaction. Does not. An interface variable has no knowledge of methods declared outside
of the interface class - for instance in the derived class. So it seems to
me that (and I haven''t seen your code) you are attempting to access a method
whose signature does not sit inside the interface class proper.

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/blogs/Alvin/
-------------------------------------------------------

"David Thielen" <th*****@nospam.nospam> wrote in message
news:BC**********************************@microsof t.com... Hi;

I am writing a class that implements IDbConnection. The i/f defines a method BeginTransaction() that returns an IDbTransaction. I want to define this as returning a DbTransaction (as DbConnection does). It makes sense to me that this is legal as DbTransaction implements IDbTransaction so it does meet the contract of returning an IDbTransaction.

But it won''t compile - how do I get this to work? There must be a way as
DbConnection does it.

I tried explicit/implicit but got an error message saying that could only be used with operators.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



你能发布一些代码,以便我们看到你在做什么吗?这是很难想象的...... b / b
Could you post some code so that we can see what you''re doing? It''s
kind of hard to visualize....


HI,


如果您发布代码以及编译器错误,您将得到很多

的精确答案。


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


" ; David Thielen <第***** @ nospam.nospam>在留言中写道

新闻:BC ********************************** @ microsof t.com ...


If you post the code as well as the compiler error you will get a much
precise answer.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"David Thielen" <th*****@nospam.nospam> wrote in message
news:BC**********************************@microsof t.com...
嗨;

我正在编写一个实现IDbConnection的类。 i / f定义了一个返回IDbTransaction的BeginTransaction()方法。我想定义这个
作为返回DbTransaction(如DbConnection那样)。这对我来说是有意义的
这是合法的,因为DbTransaction实现了IDbTransaction,因此它确实符合返回IDbTransaction的
合同。

但它不会编译 - 我如何让它工作?必须有一种方式,因为DbConnection会这样做。

我尝试了显式/隐式,但得到了一条错误消息,说明只能与运营商一起使用。

-
谢谢 - 戴夫
david_at_windward_dot_net
http://www.windwardreports.com



这篇关于再问:实现接口,返回子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆