我使用Factory模式來隱藏一些實例創建的複雜性。 我有這樣的:如何使用反射來建立通用的子類型?
class FooInt extends Foo<int>;
我想做的事:
class<Foo<?>> fooType = FooInt.class;
所有派生類型的Foo有一個構造函數取兩個參數。所以,我需要使用反射來創建子類型的實例:
Constructor<Foo<?>> ctor = fooType.getContructor(Blah.class, Blahblah.class);
return ctor.newInstance(blah, blahblah);
但javac的說:
"Type mismatch: cannot convert from Class<FooInt> to Class<Foo<?>>"
我在做什麼錯?這樣可以使用反射嗎?
不,這導致「語法錯誤令牌‘延伸’......」 – retrodrone 2011-04-01 15:03:15
@Gnat - 你是對的 – Ralph 2011-04-01 15:06:09