本文介绍了从(相同)接口派生的两种类型之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个接口和从中派生的两种类型。
I have an interface and two types that derive from it.
但是,我不能执行以下操作:
However, I cannot do the following:
B objectB = (B) objectA
其中B从Interface1派生(我正在补全类的名称,但要点仍然成立),对于objectA(类型A)同样如此。我收到以下错误消息:
Where B derives from Interface1 (I am making up the name of classes but the point still stands), and likewise for objectA (which is of type A). I get the following error message:
这两种类型都是从接口派生的,我缺少什么?
Both types are deriving from the interface, what am I missing?
推荐答案
- 类型不能从接口派生。它们实现接口。
- 大象和蜘蛛都是动物,这并不意味着您可以将一个
转换为另一个。
- Types do not derive from an interface. They implement an interface.
- The fact that both an Elephant and a Spider are Animals doesn't meanthat you can convert one to the other.
这篇关于从(相同)接口派生的两种类型之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!