3
我有一堂課Foo<T>
。我怎麼能說,我想T
是一些實施BarInterface
類?寫作只是class Foo<T implements BarInterface>
不會編譯。如何限制在泛型類中允許爲「T」的類型?
我有一堂課Foo<T>
。我怎麼能說,我想T
是一些實施BarInterface
類?寫作只是class Foo<T implements BarInterface>
不會編譯。如何限制在泛型類中允許爲「T」的類型?
使用extends
而不是implements
。
......如果我手頭有一個編譯器,我可以馬上測試它:它不應該是'class Foo <?擴展BarInterface>' - 'extends'是[WildcardBound](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1)... – 2010-07-20 08:13:11
+1註釋補充:在泛型上下文中的擴展既有'擴展'又有'實現'的含義,我在開始時發現它很混亂。 – helpermethod 2010-07-20 08:13:32
@Andreas:不,它不適用於通配符,它也適用於模板參數。 – 2010-07-20 11:04:24