Вопрос

I would like to do something like this:

if (condition)
    super(foo.class);
else
    super(bar.class);

But the super constructor has to be the first in the constructor. Is it possible to do this anyway?

Это было полезно?

Решение

Assuming you're calling the same superconstructor in both cases and just passing in a different argument, you can just use the conditional operator:

super(condition ? Foo.class : Bar.class);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top