Method overloading (przeciążenie metody)

Przeciążenie metody jest opcją, która umożliwia użycie tej samej nazwy metody ale z różnymi parametrami.

Przykład: utworzenie metody o nazwie calculateScore z dwoma parametrami:  pierwszy typu String o nazwie playerName, drugi parametr typu int o nazwie score. Ma wyświetlać informację: gracz o określonym imieniu zdobył podaną ilość punktów . W tym celu:

public class Main {

    public static void main(String[]args){

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player" + playerName + "scored"
                + score + "points");

    }

}

chcemy,  żeby metoda zwróciła nam wynik pomnożony przez 1000, więc:

public class Main {

    public static void main(String[]args){

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player" + playerName + "scored"
                + score + "points");
        return score*1000;

    }

}

określamy parametry metody calculateScore w metodzie main:

public class Main {

    public static void main(String[]args){
        calculateScore("Tim", 500);

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player" + playerName + "scored"
                + score + "points");
        return score*1000;

    }

}

po uruchomieniu powyższego kodu, w oknie konsoli wyświetla się:

Player Tim scored 500 points.

Jeśli chcemy, żeby została zwrócona wartość int , należy zadeklarować zmienną typu int o nazwie np. newScore, która będzie zwrócona z metody calculateScore:

public class Main {

    public static void main(String[]args){
       
        int newScore = calculateScore("Tim",500);
        System.out.println("New score is " + newScore);

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player " + playerName + " scored "
                + score + " points ");
        return score*1000;

    }
}

wówczas w oknie konsoli wyświetla się:

Player Tim scored 500 points
New score is 500000

 

żeby przeciążyć metodę, a więc użyć metody o te samej nazwie, ale z innymi parametrami należy:

public class Main {

    public static void main(String[]args){

        int newScore = calculateScore("Tim",500);
        System.out.println("New score is " + newScore);
      
    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player " + playerName + " scored "
                + score + " points ");
        return score*1000;

    }
    public static int calculateScore( int score){
        System.out.println("Unnamed player scored "
                + score + " points ");
        return score*1000;

jeśli chcemy przywołać drugą metodę calculateScore z jednym parametrem int (którego wartość przyjmijmy 75),  musimy zadeklarować to w metodzie main:

public class Main {

    public static void main(String[]args){

        int newScore = calculateScore("Tim",500);
        System.out.println("New score is " + newScore);
        calculateScore(75);

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player " + playerName + " scored "
                + score + " points ");
        return score*1000;

    }
    public static int calculateScore( int score){
        System.out.println("Unnamed player scored "
                + score + " points ");
        return score*1000;

 

wówczas w oknie konsoli wyświetla się:

Player Tim scored 500 points
New score is 500000
Unnamed player scored 75 points

 

utwórzmy teraz metodę o takiej samej nazwie: calculateScore bez żadnych parametrów:

public class Main {

    public static void main(String[]args){

        int newScore = calculateScore("Tim",500);
        System.out.println("New score is " + newScore);
        calculateScore(75);
        calculateScore();

    }

    public static int calculateScore(String playerName, int score){
        System.out.println("Player " + playerName + " scored "
                + score + " points ");
        return score*1000;

    }
    public static int calculateScore( int score){
        System.out.println("Unnamed player scored "
                + score + " points ");
        return score*1000;

    }
    public static int calculateScore(){
        System.out.println("No player name, no score.");
        return 0;

}

po uruchomieniu powyższego kodu wyświetla się:

Player Tim scored 500 points
New score is 500000
Unnamed player scored 75 points
No player name, no score.