Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    3

    [JAVA] HELP Array di oggetti

    Ciao a tutti.. Sono un pò in difficoltà con una questione che pensavo fosse molto più semplice..

    Allora..

    In parole povere, devo creare 3 oggetti (di tipo Player) caratterizzati dal nome (playerName). Devo poi instanziarli in un array per poi poter stampare i nomi di tutti i player inseriti..

    public class Game {

    static Player allElements[] = new Player[4];

    static void addPlayer(int itemNumber, Player player){
    allElements[itemNumber-1] = player;
    String name = Player.getPlayerName();
    System.out.println(name);
    }
    }

    public static void main (String arg []){

    Player player1 = new Player("Matteo");
    Player player2 = new Player("Giovanni");
    Player player3 = new Player("Pietro");

    Game.addPlayer(1, player1);
    Game.addPlayer(2, player2);
    Game.addPlayer(3, player3);

    }

    }

    class Player {

    static String playerName;

    Player (String enteredPlayerName) {
    setPlayerName(enteredPlayerName);

    }

    static String getPlayerName(){
    return playerName;
    }

    void setPlayerName (String enteredPlayerName){
    playerName = enteredPlayerName;
    }

    }

    Tutto questo dovrebbe stampare semplicemente:

    Matteo
    Giovanni
    Pietro

    Invece mi stampa:

    Pietro
    Pietro
    Pietro

    ..grazie a tutti quelli che mi aiuteranno..

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254

    Re: [JAVA] HELP Array di oggetti

    Originariamente inviato da valse10
    Tutto questo dovrebbe stampare semplicemente:

    Matteo
    Giovanni
    Pietro

    Invece mi stampa:

    Pietro
    Pietro
    Pietro
    Non è una questione di array ... sebbene quel tuo addPlayer() sia discutibile.
    La questione principale è il "design" delle tue classi e in particolare di Player. Perché il campo playerName e il getPlayerName() sono static??? Non ha senso per quello che dovresti fare.

    Fai semplicemente una classe tale per cui ogni sua istanza ha delle "proprietà" (nel tuo caso solo playerName) e questo presuppone che siano appunto "di istanza" .... non "di classe". Quindi togli gli static da Player!!!
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    3
    class Player {

    String playerName;

    Player (String enteredPlayerName) {
    setPlayerName(enteredPlayerName);

    }

    String getPlayerName(){
    return playerName;
    }

    void setPlayerName (String enteredPlayerName){
    playerName = enteredPlayerName;
    }
    }

    ..una cosa del genere?

    ..se lascio così però ho questo errore:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game

    at Game.main(Game.java:26)


  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Originariamente inviato da valse10
    ..una cosa del genere?


    Originariamente inviato da valse10
    ..se lascio così però ho questo errore:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game
    Cannot make a static reference to the non-static method addPlayer(int, Player) from the type Game
    Ovviamente non devi più fare:

    String name = Player.getPlayerName();

    (così invoca su Player ... il nome della classe. Che vale per cose "statiche").

    Ma semplicemente:

    String name = player.getPlayerName();

    (così invoca su player ... il parametro 'player' del metodo che è un reference di tipo Player)
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Mar 2009
    Messaggi
    3
    Ehi.. grazie mille..

    Problema risolto..

    Tutto funziona alla grande..



Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.