【问题标题】:how to convert object of one type(Interface) to another(Interface)?如何将一种类型(接口)的对象转换为另一种(接口)?
【发布时间】:2014-06-03 05:39:08
【问题描述】:

我有一个接口 Interface1,它已由 A 类实现,并设置了一些私有变量值,m 将 A 类的对象发送到下一个接受作为 Interface2 输入的类。那么如何将这个引用 Interface1 的对象转换为 Interface2?

interface Interface1{}
interface Interface2{}

class A implements Interface1 {
    private int id;

    public void setId(int id)
    {
        this.id = id;
    }
}

class B {
    Interface1 call(Interface1 i)
    {
        C c = new C();
        c.call2(i); //this should be of type Interface2 so how to do this
        return i;
    }

    public static void main(String arg[])
    {
        Interface1 i = new A();
        i.setId(5);
        B b = new B();
        b.call(i);
    }
}

class c {
    Interface2 call2(Interface2 i)
    {
        return i;
    }
}

【问题讨论】:

  • 我不认为你可以从 interface1 转换为 interface2,除非一个继承自另一个。转换语法通常是 (interface2) xyz;
  • 这两个接口有什么关联吗?你代码的意图不是很清楚。你到底想达到什么目的?

标签: java interface


【解决方案1】:

鉴于Interface1Interface2 是两个完全不相关的接口,如何假设A 实例能够用作Interface2A 甚至可能没有定义 Interface2 方法。

您可以通过多种方式使这个设计更合适。

首先,如果要将 Interface1 和 2 保留为两个不相关的接口,则应该让 A 实现它们:

class A implements Interface1, Interface2 {

至少您可以在B#call() 中进行一些转换:

if (i instanceof Interface2) {
    Interface2 i2 = (Interface2) i;
    ....
    c.call2(i2);
}

如果在你的设计中让Interface1和2有继承关系是合适的,故事可能会简单一些:

interface Interface1 extends Interface2 {

那么Interface1可以直接作为Interface2使用。

另一种可能性是,如果您知道,无论您通过 Interface1 提供什么,它都可以以某种方式用作 Interface2(例如,有点像 Java 的 Runnable 和 Callable),您可以为 Interface1 编写一个 Interface2 适配器:

class Interface1I2Adapter implements Interface2 {
    Interface1 i1;
    public Interface1I2Adapter (Interface1 i1) {
         this.i1 = i1;
    }
    // impl of Interface2 methods by delegating to i1
}

然后你就可以使用 by

c.call2(new Interface1I2Adapter(i));

类似的东西。

如果没有关于您的设计的更多信息,就不可能为您推荐正确的方法。以下是一些适用于很多情况的建议。

【讨论】:

  • 在尝试您的评论第二部分 (this.i1 = i1) 时,我丢失了 Interface1 中的数据。那么还有其他方法吗
  • @user3701528 你这是什么意思?你能举一个实际的例子来说明为什么你需要做所有这些事情吗?我相信这只是一些您可以使用其他方式处理的设计问题。因为你所要求的只是不合理的恕我直言
【解决方案2】:

类似于以下内容?

 class D implements Interface2{}

C c = new C();
D d = new D();
d.set...(i.get...());
c.call2(d);

【讨论】:

    【解决方案3】:

    您不能进行从一个接口到另一个接口的同上转换,因为它们是独立的抽象。或者,您可以通过将 Interface1 作为 Interface2 的子类来实现此目的。您应该按如下方式扩展 Interface2 并使用它:

      interface Interface1 extends Interface2{  
          void setId(int id);
      }
    

    【讨论】:

      【解决方案4】:

      Interface1 应该扩展 Interface2 以便将 Interface1 引用传递给 Interface2。

      正确的代码-

      interface Interface1 extends Interface2 { //Imp
      
      
      
      }
      
      interface Interface2  {
      public void setId(int id);
      }
      
      class A implements Interface1 {
      private int id;
      
      public void setId(int id) {
          this.id = id;
      }
      }
      
      public class B {
      Interface1 call(Interface1 i) {
          C c = new C();
          c.call2(i); // this should be of type Interface2 so how to do this
          return i;
      }
      
      public static void main(String arg[]) {
          Interface1 i = new A();
          i.setId(5);
          B b = new B();
          b.call(i);
      }
       }
      
      class C {
      Interface2 call2(Interface2 i) {
          return i;
      }
      }
      

      【讨论】:

        【解决方案5】:

        这类问题一般通过Composition解决。

        要组合现有接口,您需要编写一个实现 Interface2 并委托给 Interface1 内部实例的类。

        Go4 Adapter Pattern 进一步表达了这个想法。

        【讨论】:

          猜你喜欢
          • 2010-09-23
          • 2017-05-28
          • 2012-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-08
          • 1970-01-01
          相关资源
          最近更新 更多