【发布时间】: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;
-
这两个接口有什么关联吗?你代码的意图不是很清楚。你到底想达到什么目的?