如何从另一个 class 获取存储在 hashmap 中的数据

How to get the data stored inside the hashmap from another class

我是 Java 开发的新手并且还在学习,现在我一直在从另一个 class.

获取 HashMap 的数据

基本上我有一个 class 包含我的哈希图,这个 class 将只有哈希图和列表来存储我的数据,然后另一个 class 将得到这个哈希图并存储一些数据到它,之后另一个 class 需要访问 hashmap 以获取存储给它们的一些值。

到目前为止我只知道访问那些HashMap的三种方法

  1. 我想通过使用 extend 关键字将 hashmap class 扩展到我的另一个 class?还没试过
  2. 通过使用 static 关键字,我以前使用的那个基本上是在滥用 static 关键字,我现在也在努力避免。
  3. 最后是通过获取那个 class 的实例吗?这就是我现在使用的问题

为了解释这个问题,我有这个数据class,它将包含我所有的哈希图

public class getLocation {
   HashMap<String, Location> location = new HashMap<>();

   public HashMap<String, Location> getloc(){
        return location;
    }
}

那么这是我的class之一,它会放一些数据

public class set1 {
   // As far as I know this will create a new instance, therefore the hashmap will be a new empty one
   private getLocation getLoc = new getLocation();

   // Putting some data
   public void setPosition() {
       getLoc.getloc().put("Position_1", Location);
   }
}

然后终于是第三个class

public class setBlock {
   // Again getting a new instance, therefore an empty hashmap again..
   private getLocation getLoc = new getLocation();

   public void getBlock() {
       System.out.println(getLoc.getloc().get("Position_1"));
   }
   
}

我认为这基本上只是 return null,因为我正在创建一个新实例,因此它将始终为空。有没有正确的方法来做到这一点?在我只是在我拥有的每个 HashMap 上使用 static 关键字之前,但我了解到这不是一个好习惯,因此我正在尝试学习正确的方法。

我在网上阅读的几乎所有东西都只是创建一个 hashmap,然后在另一个 class 上调用它并在那里设置它的数据就是这样,我需要的是在另一个 [=] 上再次访问它43=].

抱歉,如果我在解释事情时变得多余,我只是想尽我所能解释它。

如果您对如何正确执行此操作或其他操作有任何建议,请帮助我

一般来说,您不想公开您的数据存储(在本例中是您的哈希图)。相反,您希望其他对象以特定方式与您的 HashMap 交互。

这就是我认为您想要实现的目标:此 Class 的对象将为玩家存储位置,提供给定玩家的位置。

// Aside from "Start with Capital letter", the name of a Class should give
// (more than) a hint about its purpose. "getLocation" is a bad, bad, bad name
// because 1) it will not be obvious it's a class and 2) can be confused by humans
// for a method.
public class Storage {

    HashMap<Player, Location> locationStorage;

    // This is the constructor for Storage-objects.
    // It's called with the `new` keyword and creates itself 
    // a new HashMap. 
    // Each Storage-object will have its own HashMap `locationStorage`
    public Storage() {
        this.locationStorage = new HashMap<>();
    }
    
    // This is a so-called Getter-Method, that exposes the HashMap
    // to other classes in a defined way; it will retrieve the Location-Object
    // associated with the `key` in the Storage's `locationStorage`.
    public Location getLocation(Player player) {
        return this.locationStorage.get(player);
    }

    // This is a so-called Setter-Method, that exposes a way to add
    // a value to our HashMap `locationStorage`
    public void setLocation(Player player, Location loc) {
        this.locationStorage.put(player, loc);
    }

}

让我们进一步假设我们有一个 Player class 看起来像这样:

public class Player {

    private Location playerLocation;
    // ... other variables
    
    public Location getLocation() {
        return this.playerLocation;
    }

}

然后我们可以有一个 class 有你的 main() 方法:

public class MainGame {

    public static void main(String[] args) {

        // using the keyword `new` we can instantiate an Object of the following class
        Storage storage = new Storage();
        // Let's assume you have a Player class that that holds a Location
        Player player = new Player();

        // Storing the Location object of the Player object in our Storage object
        storage.setLocation(player, player.getLocation());

        // retrieving the Location of the player Player
        Location currentLocation = storage.getLocation(player);
    }
}