为什么我在这里遇到转换错误?
适配器类和目标类都实现相同的接口...为什么我不能将它们视为同一个对象?
interface ISmartPhone
{
string Name { get; set; }
string Type { get; set; }
void ShowTextWithImage();
}
public class BasicFlipPhoneAdapter : ISmartPhone
{
IBasicPhone basicPhone;
public BasicFlipPhoneAdapter(IBasicPhone basicPhone)
{
this.basicPhone = basicPhone;
}
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
basicPhone.ShowBasicText();
}
}
public class iPhone : ISmartPhone
{
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
Console.WriteLine("O.o cool image!");
}
}
出现错误:
iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
Both the adapter class and the target class implement the same interface...why can I not treat them like the same object?
interface ISmartPhone
{
string Name { get; set; }
string Type { get; set; }
void ShowTextWithImage();
}
public class BasicFlipPhoneAdapter : ISmartPhone
{
IBasicPhone basicPhone;
public BasicFlipPhoneAdapter(IBasicPhone basicPhone)
{
this.basicPhone = basicPhone;
}
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
basicPhone.ShowBasicText();
}
}
public class iPhone : ISmartPhone
{
public string Name { get; set; }
public string Type { get; set; }
public void ShowTextWithImage()
{
Console.WriteLine("O.o cool image!");
}
}
Error occurs:
iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为它们不是相同的对象类型。
BasicFlipPhoneAdapter
不是iPhone
。您应该能够做的是:记下
flipPhoneAdapter
变量的类型 - 它是ISmartPhone
,而不是iPhone
。Because they're not the same object type. A
BasicFlipPhoneAdapter
is not aniPhone
. What you should be able to do is:Note the type of the
flipPhoneAdapter
variable - it'sISmartPhone
, notiPhone
.尝试
ISmartPhone FlipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
try
ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
它们不是相同的对象。但它们可以在您的ISmartPhone 界面中进行塑造。您可以将
BasicFlipPhoneAdapter
和iPhone
实例强制转换为ISmartPhone
并使用公开的签名。They are not same objects. But they can be shaped in your ISmartPhone interface. You can cast both of your
BasicFlipPhoneAdapter
andiPhone
instance toISmartPhone
and use the exposed signature.由于两者均派生自
ISmartPhone
,因此您当然可以将BasicFlipPhoneAdapter
和iPhone
视为ISmartPhone
,例如,但是,您不能隐式地将
iPhone
视为BasicFlipPhoneAdapter
。它们具有相同的基本类型,但除此之外可能有所不同。Since both are derived from
ISmartPhone
, you can certainly treat either aBasicFlipPhoneAdapter
and aniPhone
as anISmartPhone
, e.g.,However, you cannot implicitly treat an
iPhone
as anBasicFlipPhoneAdapter
. They have the same base type, but can differ besides that.应该是
iPhone不是BasicFlipPhoneAdapter的超类型
It should be
iPhone is not a super type of BasicFlipPhoneAdapter
仅仅因为两个类实现相同的接口并不意味着它们可以互换。
无效,因为您尝试将
iPhone
类型的变量分配给BasicFlipPhoneAdapter
类的实例。另一方面,您可以拥有对任何实现该接口的类进行操作的方法。取这个函数原型:Just because two classes implement the same interface doesn't mean they're interchangeable.
is invalid because you're trying to assign a variable of type
iPhone
an instance of classBasicFlipPhoneAdapter
. On the other hand, you can have methods which operate on any class which implements the interface. Take this function prototype:我们需要更多有关 ISmartPhone 扩展内容的详细信息才能确定答案,但看起来您试图说所有可能是“flipPhone”的项目都是 iPhone。仅基于您提供的代码,我假设所有“flipPhone”实例都是 ISmartPhone,因此如果您更改
为
它应该可以工作。或者,如果 ISmartPhone 确实扩展了 IBasicPhone,则将 FlipPhoneAdapeter 的类型更改为 IBasicPhone。
We need more detail on what ISmartPhone extends to answer for sure but it looks like you are trying to say that all items that could be "flipPhone" are iPhone. Based on just the code you provided I'm assuming all "flipPhone" instances would be an ISmartPhone so if you change
to
it should work. Or if ISmartPhone does extend IBasicPhone then change the type of flipPhoneAdapeter to IBasicPhone.