为什么我在这里遇到转换错误?

发布于 2024-12-15 04:56:35 字数 851 浏览 6 评论 0原文

适配器类和目标类都实现相同的接口...为什么我不能将它们视为同一个对象?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤千羽 2024-12-22 04:56:35

因为它们不是相同的对象类型。 BasicFlipPhoneAdapter 不是 iPhone。您应该能够做的是:

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

记下flipPhoneAdapter变量的类型 - 它是ISmartPhone,而不是iPhone

Because they're not the same object type. A BasicFlipPhoneAdapter is not an iPhone. What you should be able to do is:

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

Note the type of the flipPhoneAdapter variable - it's ISmartPhone, not iPhone.

成熟的代价 2024-12-22 04:56:35

尝试ISmartPhone FlipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

try ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

差↓一点笑了 2024-12-22 04:56:35

它们不是相同的对象。但它们可以在您的ISmartPhone 界面中进行塑造。您可以将 BasicFlipPhoneAdapteriPhone 实例强制转换为 ISmartPhone 并使用公开的签名。

They are not same objects. But they can be shaped in your ISmartPhone interface. You can cast both of your BasicFlipPhoneAdapter and iPhone instance to ISmartPhone and use the exposed signature.

何处潇湘 2024-12-22 04:56:35

由于两者均派生自 ISmartPhone,因此您当然可以将 BasicFlipPhoneAdapteriPhone 视为 ISmartPhone,例如,

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

但是,您不能隐式地将 iPhone 视为 BasicFlipPhoneAdapter。它们具有相同的基本类型,但除此之外可能有所不同。

Since both are derived from ISmartPhone, you can certainly treat either a BasicFlipPhoneAdapter and an iPhone as an ISmartPhone, e.g.,

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

However, you cannot implicitly treat an iPhone as an BasicFlipPhoneAdapter. They have the same base type, but can differ besides that.

暗地喜欢 2024-12-22 04:56:35

应该是

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

iPhone不是BasicFlipPhoneAdapter的超类型

It should be

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

iPhone is not a super type of BasicFlipPhoneAdapter

银河中√捞星星 2024-12-22 04:56:35

仅仅因为两个类实现相同的接口并不意味着它们可以互换。

iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

无效,因为您尝试将 iPhone 类型的变量分配给 BasicFlipPhoneAdapter 类的实例。另一方面,您可以拥有对任何实现该接口的类进行操作的方法。取这个函数原型:

public void DoSomethingPhoneLike(ISmartPhone phone) {
    // ... do things with phone, like phone.ShowTextWithImage()
}

Just because two classes implement the same interface doesn't mean they're interchangeable.

iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);

is invalid because you're trying to assign a variable of type iPhone an instance of class BasicFlipPhoneAdapter. On the other hand, you can have methods which operate on any class which implements the interface. Take this function prototype:

public void DoSomethingPhoneLike(ISmartPhone phone) {
    // ... do things with phone, like phone.ShowTextWithImage()
}
很酷又爱笑 2024-12-22 04:56:35

我们需要更多有关 ISmartPhone 扩展内容的详细信息才能确定答案,但看起来您试图说所有可能是“flipPhone”的项目都是 iPhone。仅基于您提供的代码,我假设所有“flipPhone”实例都是 ISmartPhone,因此如果您更改

iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone); 

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone); 

它应该可以工作。或者,如果 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

iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone); 

to

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone); 

it should work. Or if ISmartPhone does extend IBasicPhone then change the type of flipPhoneAdapeter to IBasicPhone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文