Java 如何使用另一个类中的变量?

Java 如何使用另一个类中的变量?,java,Java,我正在制作一个迷宫游戏。为此,我创建了4个独立的类文件。我想知道如何在Player.class move()方法中访问Maze.class的囚犯,并不仅阅读它,还可以添加和覆盖它: 迷宫类 package y1; import java.util.*; @SuppressWarnings(value = "all") public class Maze { private Room entry; private Map <Room, ArrayList<Player>&g

我正在制作一个迷宫游戏。为此,我创建了4个独立的类文件。我想知道如何在Player.class move()方法中访问Maze.class的囚犯,并不仅阅读它,还可以添加和覆盖它:

迷宫类

package y1;

import java.util.*;

@SuppressWarnings(value = "all")

public class Maze {
private Room entry;
private Map <Room, ArrayList<Player>> inmates=new HashMap<Room,ArrayList<Player>>();
/**
 * 
 * @param r The room that is going to be the entry
 */
public void setEntry(Room r){
    this.entry=r;
    }
/**
 * 
 * @return Returns the entry room
 */
    public Room getEntry(){
        return this.entry;
    }
    /**
     * Method adds a player to the maze
    * @param p Player to be added to the maze
    */
    public void addPlayer(Player p){
        ArrayList<Player> players=new ArrayList<Player>();
        p.setRoom(getEntry());
        if (this.inmates.isEmpty()==true){
        players.add(p);
        this.inmates.put(this.entry,players);
        }
        else{
        players=this.inmates.get(this.entry);
        players.add(p);
        }

        this.inmates.put(p.getRoom(), players);

    }
    public void getPlayers(Room r){
        if(this.inmates.get(r)!=null){
        System.out.println(Arrays.asList(this.inmates.get(r)));
        }
        else{
        System.out.println("Ruum on tühi");

        }
    }
    public Map getInmates(){
        return this.inmates;
    }
}
}

还有球员

package y1;

import java.util.*;

public class Player {
private String name;
private Room location;

public void setRoom(Room r){
    this.location=r;
}
public Room getRoom(){
    System.out.println(this.name+" is at " +this.location);
    return this.location;

}


public Room move(String dir){
    Player p=this;
    if(dir=="N" && this.location.getNorth()!=null){
        p.location=p.location.getNorth();
    }
    else if(dir=="S" && this.location.getSouth()!=null){
        p.location=p.location.getSouth();
    }
    else if(dir=="W" && this.location.getWest()!=null){
        p.location=p.location.getWest();
    }
    else if(dir=="E" && this.location.getEast()!=null){
        p.location=p.location.getEast();
    }
    else{
        System.out.println("There's a wall in the way!");
    }
    return this.location;   
}
public Room move(List<String> dirList){
    Player player=this;
        for(int i=0 ; i<dirList.size() ; i++){
            if(dirList.get(i)=="N" && player.location.getNorth()!=null){
                player.location=player.location.getNorth();
            }
            else if(dirList.get(i)=="S" && player.location.getSouth()!=null){
                player.location=player.location.getSouth();
            }
            else if(dirList.get(i)=="W" && player.location.getWest()!=null){
                player.location=player.location.getWest();
            }
            else if(dirList.get(i)=="E" && player.location.getEast()!=null){
                player.location=player.location.getEast();
            }   
            else{
                System.out.println("Wall in the way. Stopping!");
                break;
            }
        }
    return this.location;   
}

public void setName(String n){
    this.name=n;
}


@Override
public String toString() {
    return this.name;
}
y1包;
导入java.util.*;
公开课选手{
私有字符串名称;
私人房间位置;
公共休息室(r室){
这个位置=r;
}
公共休息室{
System.out.println(this.name+”位于“+this.location”);
返回此位置;
}
公共房间移动(字符串方向){
玩家p=这个;
if(dir==“N”&&this.location.getNorth()!=null){
p、 location=p.location.getNorth();
}
else if(dir==“S”&&this.location.getSouth()!=null){
p、 location=p.location.getSouth();
}
else if(dir==“W”&&this.location.getWest()!=null){
p、 location=p.location.getWest();
}
else if(dir==“E”&&this.location.getEast()!=null){
p、 location=p.location.getEast();
}
否则{
System.out.println(“有堵墙挡住了去路!”);
}
返回此位置;
}
公共房间移动(列表){
玩家=这个;

对于(inti=0;i您需要创建修饰符方法,例如

public void resetInmates(){
    this.inmates = new Map();
}

public void addInmate( Room r, ArrayList<Player> players ){
}

public void removeInmate( Room r ){
}
public void reset囚犯(){
this.incomates=新地图();
}
公共无效附加项(r室,ArrayList玩家){
}
公共空间移除(r室){
}

etc

您需要创建修饰符方法,例如

public void resetInmates(){
    this.inmates = new Map();
}

public void addInmate( Room r, ArrayList<Player> players ){
}

public void removeInmate( Room r ){
}
public void reset囚犯(){
this.incomates=新地图();
}
公共无效附加项(r室,ArrayList玩家){
}
公共空间移除(r室){
}

etc

只需将
private
从Inmaints字段中删除,就可以使同一个包中的其他类可读,但是创建一个getter方法(可能也可以看到包)是一个很好的设计相反。

只需从Inmanates字段中删除
private
,就可以使同一个包中的其他类可读,但最好是创建一个getter方法(可能也可以看到包)。

玩家需要以某种方式引用迷宫对象,以便调用getInmanates()方法(或任何其他修改器方法)

如何获取引用取决于您,有很多方法可以实现。一种方法是让玩家的位置(房间对象)提供引用。为此,您可以在房间对象中实现一个getMaze()方法,该方法将返回该房间的迷宫。在Player的move()方法中,您可以调用getMaze()在玩家的“location”属性(这是一个房间)上,然后对返回的迷宫对象调用getInmates()


在这之后,您需要在迷宫对象上实现修改器/重置方法。

玩家需要以某种方式对迷宫对象进行引用,以便在其上调用getInmates()方法(或任何其他修改器方法)

如何获取引用取决于您,有很多方法可以实现。一种方法是让玩家的位置(房间对象)提供引用。为此,您可以在房间对象中实现一个getMaze()方法,该方法将返回该房间的迷宫。在Player的move()方法中,您可以调用getMaze()在玩家的“location”属性(这是一个房间)上,然后对返回的迷宫对象调用getInmates()



在这之后,这就是您想要在迷宫对象上实现修改器/重置方法的方式。

是的,可能比getter方法更好,这在一定程度上取决于其他类需要多少访问权限。请注意,您可能不想使用
public
公开这些方法。如果这些方法不可用,包可见性应该足够了用于在包外访问。是的,可能比getter方法更好,这取决于其他类需要多少访问权限。请注意,您可能不希望使用
public
公开这些方法。如果这些方法不用于在包外访问,包的可见性应该足够了。这是一个非常糟糕的practice;其他类不需要担心如何实现
囚犯
以及如何读取/访问/修改它们的细节,但它确实回答了这个问题。此外,如果它隐藏在包中而不暴露于外部,它可能有它的merrits。我已经提到,scibuff的答案更好。我这是一个非常糟糕的做法;其他类不需要担心如何实现
囚犯
以及如何读取/访问/修改它们的细节,但它确实回答了这个问题。此外,如果它隐藏在一个包中而不暴露于外,它可能有它的merrits。我已经提到了scibuff I的答案好多了。谢谢!这就是我一直在寻找的解决方案!真不敢相信它就在我面前。我觉得我不必再处理房间类的问题了。但似乎没有什么可以逃避的:我仔细考虑了,意识到对我来说最好的办法是给玩家分配另一个名为“mazeloc”的位置属性或者类似的东西。当使用addPlayer()方法时,它将被初始化。此属性显示播放器当前处于哪个迷宫。@Rauno,这是可行的,但是,就“正确”设计而言,这可能不是最好的方法。问题是您引入了更多的“簿记”通过这样做:现在你需要在玩家每次移动时除了“位置”之外更新“mazeloc”。事实上不是。mazeloc只是用来保存玩家当前所在迷宫的信息。它只在玩家退出迷宫并进入另一个迷宫时才会更改。啊。不过,它还是一个额外的属性,可以跟踪玩家当前所在的迷宫,而不必进行任何修改从现有属性中进行了大量工作。例如,可能您将迷宫拆分为不同的子迷宫。每次玩家移动位置时,他们都需要检查他们所在的迷宫是否已更改,并更新他们的“迷宫”