How to call a different constructor conditionally in Java?

后端 未结 5 1535
不思量自难忘°
不思量自难忘° 2020-12-11 16:16

Let\'s say someone gives you a class, Super, with the following constructors:

public class Super
{
    public Super();
    public Super(int arg)         


        
5条回答
  •  误落风尘
    2020-12-11 16:30

    You can't do that, but you can do this from the code that calls your class:

            if (some_condition_1)
                new Super();
            else if (some_condition_2)
                new Super("Hi!");
            else if (some_condition_3)
                new Super(new int[] { 5 });
            else
                new Super(arg);
    

提交回复
热议问题