Java: How to set a default for “T” in SomeClass?

后端 未结 4 1687
走了就别回头了
走了就别回头了 2020-12-11 14:35

Is there a way to specify a default type for a generic template?

Let\'s say I have a Monkey class. Monkeys can live in different Environment

4条回答
  •  死守一世寂寞
    2020-12-11 15:20

    No, you can't do that. Generic parameters don't have default values. You could re-organize your type hierarchy so that there's a GenericMonkey and a DefaultMonkey that sets the generic parameter to your desired default.

提交回复
热议问题