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.
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.
public interface U {
void m1(); void m2(); void m3();
}class A {
public U getU() {
return new U() {
public void m1() {
} public void m2() {
} public void m3() {
}
};
}
}class B {
U[] uArrays = null; public void setUArrays(U[] arrays) {
uArrays = arrays;
} public void setNull() {
setUArrays(null);
} public void test() {
for (int i = 0; i < uArrays.length; i++) {
uArrays[i].m1();
uArrays[i].m2();
uArrays[i].m3();
}
} public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
B b = new B();
b.setUArrays(new U[] { a1.getU(), a2.getU() });
b.test();
b.setNull();
}
}
这句话我感觉我理解的有问题,希望en好的,帮我确认下,感觉我上面的call back很恶心,不像是一个设计,
完全是我根据题意的单纯翻译。
class A {
public U getU() {
final A a = this;
return new U() {
public void m1() {
System.out.println("有点意思"+a);
} public void m2() {
} public void m3() {
}
};
}
}
accepts and stores a reference to a U in the array,
看这个,总觉得是
public void setUArrays(U u) {
uArray[count++]=u;
}
对这句的理解我觉得是这样的:
private U u = new U() { public void m1() {
// TODO Auto-generated method stub
} public void m2() {
// TODO Auto-generated method stub
} public void m3() {
// TODO Auto-generated method stub
}
};