Create an interface U with three methods. Create a class A with a method that produces a reference to a U by building an anonymous inner class. Create a second class B that contains an array of U. B should have one method that accepts and stores a reference to a U in the array, a second method that sets a reference in the array (specified by the method argument) to null, and a third method that moves through the array and calls the methods in U. In main( ), create a group of A objects and a single B. Fill the B with U references produced by the A objects. Use the B to call back into all the A objects. Remove some of the U references from the B.
那不可能吧? 数组必须存储对象的实例 接口是抽象的 不会有实例的 更不要说存储接口了 我是这么理解的 楼下看看有没有办法
package Test;
import java.util.*;
public class qdbcool
{
private Animal a;
public Object[] fill(int length)
{
Object[] os=new Object[length];
for(int i=0;i<os.length;i++)
{
os[i]=a;
}
return os;
}
public void setAnimal(Animal a)
{
this.a=a;
}
public static void main(String[] args)throws Exception
{
qdbcool q=new qdbcool();
System.out.println(Arrays.toString(q.fill(10)));
q.setAnimal(new Dog());
System.out.println(Arrays.toString(q.fill(10)));
}
}
interface Animal{}
class Dog implements Animal{}
class Pig implements Animal{}正如test所见,数组其实保存的是一个引用,这引用甚至还没指向新的实例。
那问题:接口有它自身的实例么?
U接口的实例
that produces a reference to a U by building an anonymous inner class. Create a
second class B that contains an array of U. B should have one method that
accepts and stores a reference to a U in the array, a second method that sets a
reference in the array (specified by the method argument) to null, and a third
method that moves through the array and calls the methods in U. In main( ),
create a group of A objects and a single B. Fill the B with U references
produced by the A objects. Use the B to call back into all the A objects. Remove
some of the U references from the B.
创建一个有3个方法的接口U。 创建一个类A,并且A中有一个方法,在这个方法里通过创建匿名内部类来产生一个U的引用。创建第二个类B,在B中有一个U型的数组,而且B中有一个方法,它能将U的引用接受并存储在数组中;另一个方法...通过这个,我觉得是接口类型的数组 而不是数组来存储接口...
l.add();
.
.
.
u[1]=A.u()
其中public U u(){
return new u(){
public void value();
}}
有错,就是不知道怎么办?