2016-04-01 11 views
0

Ich habe diese Person-Klasse mit einigen Methoden zum Hinzufügen, Entfernen und Anzeigen Ihrer Freunde. Aus irgendeinem Grund, wenn ich meinen Freund sehen möchte, obwohl ich glaube, dass ich die richtigen Methoden richtig rufe, wird das Objekt der letzten Person (welches carl ist) unabhängig von den Objektspezifikationen im Methodenaufruf gedruckt. Was ist das Problem?letzte Objekt wird unabhängig gedruckt

hier ist der Code:

package person; 

public class Person { 
    private static String name; 
    private static String friends; 

    public static void main(String[] args) { 

     Person ted = new Person ("ted"); 
     Person jim = new Person ("jim"); 
     Person todd = new Person ("todd"); 
     Person tom = new Person ("tom"); 
     Person carl = new Person ("carl"); 

     // apparently I'm making a mistake here... 
     jim.addFriend(zack); 
     System.out.println(jim.getFriends()); 
    } 
    public Person (String aName) { 
     name = aName; 
     friends = ""; 
    } 
    public static void addFriend(Person friend) { 

     friends = friends + friend.name + " "; 
    } 
    public static void unFriend (Person nonFriend) { 
     friends = friends.replace(nonFriend.name + " ", ""); 
    } 
    public static String getFriends() { 
     return friends; 
    } 

} 
+0

Entfernen Weil sie 'sind, dass das Entfernen static' zu versuchen. 'Static' bedeutet ziemlich genau, dass es für alle Instanzen, die von dieser Klasse erstellt wurden, gleich ist. – 3kings

+0

Entfernen Sie alle statischen außer Main. Ein statisches Feld wird nur einmal existieren, ist ein Feld des Klassenobjekts, und man könnte 'Person.name =" any ";' –

+0

thanks! das war es – zamzam

Antwort

3

Sie verwenden statische Variablen. Bitte versuchen Sie es.

package person; 

public class Person { 
    private String name; 
    private String friends; 

    public static void main(String[] args) { 

     Person ted = new Person ("ted"); 
     Person jim = new Person ("jim"); 
     Person todd = new Person ("todd"); 
     Person tom = new Person ("tom"); 
     Person carl = new Person ("carl"); 

     // apparently I'm making a mistake here... 
     jim.addFriend(zack); 
     System.out.println(jim.getFriends()); 
    } 
    public Person (String aName) { 
     name = aName; 
     friends = ""; 
    } 
    public void addFriend(Person friend) { 

     friends = friends + friend.name + " "; 
    } 
    public void unFriend (Person nonFriend) { 
     friends = friends.replace(nonFriend.name + " ", ""); 
    } 
    public String getFriends() { 
     return friends; 
    } 

} 
0

Machen

private static String name; 
private static String friends; 

Um

private String name; 
private String friends; 

und Methoden ändern, indem static zu

public void addFriend(Person friend) { 

     friends = friends + friend.name + " "; 
    } 
    public void unFriend (Person nonFriend) { 
     friends = friends.replace(nonFriend.name + " ", ""); 
    } 
    public String getFriends() { 
     return friends; 
    } 
+0

ja war ich redigierend –

Verwandte Themen