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

dbo*_*ski 0 c# type-conversion adapter

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

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

发生错误:

iPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 8

因为它们不是同一个对象类型.一个BasicFlipPhoneAdapter没有iPhone.你应该做的是:

ISmartPhone flipPhoneAdapter = new BasicFlipPhoneAdapter(flipPhone);
Run Code Online (Sandbox Code Playgroud)

注意flipPhoneAdapter变量的类型- 它ISmartPhone不是iPhone.