문제

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