有什么方法可以从MainClass更改已创建用户的值?

时间:2019-08-03 17:44:25

标签: java constructor hashmap

我无法更改在Main类中创建的对象的值。

它使用java jre8。我已经在Eclipse IDE上尝试过。

导入java.util.HashMap;

公共类主要{

public static HashMap<String,Data> PlayerData = new HashMap<String,Data>();

public static void main(String[] args) {
    PlayerData.put("User1", new Data("User1","Password1",21));
    PlayerData.get("User1").giveInformation();
    PlayerData.get("User1").setAge(78);
    try{
        PlayerData.get("User1").giveInformation();
    }catch(NullPointerException e) {
        System.out.println("Error!");
    }catch(Exception e) {
        System.out.println("Error!");
    }
}

}

类数据{

private String Name;
private String Password;
private int Age;

public Data(String Name, String Password, int Age) {
    this.setName(Name);
    this.setPassword(Password);
    this.setAge(Age);
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public int getAge() {
    return Age;
}

public void setAge(int age) {
    Age = age;
}

public void giveInformation() {
    System.out.println("Name: " + this.Name);
    System.out.println("Password: " + this.Password);
    System.out.println("Age: " + this.Age);
}

} 我想运行giveInformation()方法来查看卡路里变化,但是我不知道该怎么做。

0 个答案:

没有答案