Defining array of unknown type of objects and assign the correct type of objects in the constructor
I am trying to achieve generic behavior of foo_unknown class, which calls foo_func1(), foo_func2() methods; and it depends on the type of objects defined in the constructor of foo class that which foo_func1() and foo_func2() method implementations are called. Any new revolutionary design pattern is welcome 开发者_StackOverflow中文版too. Which takes care of all the issues.
If you are not averse to doing this reflectively, try something like this:
public FooController(Class<? extends Foo> clazz, int number) {
this.fooUnknown = Array.newInstance(clazz, number);
for (int i = 0; i < number; i++) {
Array.set(this.fooUnknown, i, clazz.newInstance());
}
}
(Warning: not compiled, not tested, requires exception handling, etc)
精彩评论