Can I have more than one builder per class?

Asked

Viewed 1,765 times

4

The question is simple personal: I can have more than one constructor in the same class?

At first, I will need to define the variable plugin in my object, then I will use the constructor that defines such a variable;

public RankAPI(Main main) {

    plugin = main;

}


However, in a second moment, I will need to define a second variable, the variable p on my object, then I will have to use the second constructor:

public RankAPI(Player player) {

    p = player;

}

Unfortunately, I cannot define the two variables, because the variable p will be defined by other programs when they need to access my API, and the variable plugin will be defined by my own program when executed.

Thank you for your attention.

  • Doesn’t it make more sense to separate into different classes? With this approach you always have to check which is set and which is not. I

1 answer

7


Can. Just need to set different parameters for them (in types and quantity), this is called Overload (overload).

Ex.:

class MinhaClasse
{
    private int id;
    private String nome;

    public MinhaClasse() { }

    public MinhaClasse(String nome) { this.nome = nome; }

    public MinhaClasse(int id) { this.id = id; }

    public MinhaClasse(int id, String nome) { this.id = id; this.nome = nome; }
}
  • Great, so I’m doing the right thing :) Thank you!

  • 1

    It is worth remembering that delegates are different including by quantity, not only types ...!

  • 2

    Muchas Gracias.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.