在Java中从类到类传递变量

在Java中从类到类传递变量,java,class,variables,inheritance,Java,Class,Variables,Inheritance,在完成一个基于垄断类型文本的游戏时,我在Java中从一个类到另一个类传输变量数据时遇到了一些问题 我一直在尝试将roll1和roll2变量从Dice类传输到Board类,但由于某些原因,数据传输不正确,当我从Board类中的roll2返回组合数据时,它返回为0 以下是我的课程: public class Dice { static int dots, roll1, roll2, flag; Random number = new Random(); public Di

在完成一个基于垄断类型文本的游戏时,我在Java中从一个类到另一个类传输变量数据时遇到了一些问题

我一直在尝试将roll1和roll2变量从Dice类传输到Board类,但由于某些原因,数据传输不正确,当我从Board类中的roll2返回组合数据时,它返回为0

以下是我的课程:

public class Dice {

    static int dots, roll1, roll2, flag;
    Random number = new Random();

    public Dice(){
         dots = number.nextInt(6)+1 ;
    }

    public void roll1(){
        roll1 = number.nextInt(dots)+1;
    }

    public void roll2(){
        roll2 = number.nextInt(dots)+1;
    }

    public int getDots1(){
        return roll1;
    }

    public int getDots2(){
        return roll2;
    }

    public String getSame(){
        if(roll1 == roll2){
            flag++;
            return("Your rolls were the same");
        }
        else{
            return(" ");
        }

    }
}


你的代码的问题是你掷骰子,但是你没有在
Board
类中更新骰子的值

这可能会解决您的问题,但不会解决您的布局问题

public class Blobfish {

    public void main(String[] args) {
        System.out.println(" " +Board.getPos());
        Dice dice = new Dice();
        dice.roll1();
        dice.roll2();
        Board.roll1 = dice.getDots1();
        Board.roll2 = dice.getDots2();
        System.out.println("First Roll: " + dice.getDots1());
        System.out.println("Second Roll: " + dice.getDots2());
        System.out.println(dice.getSame());
        System.out.println("Your Current Position = " + Board.getPos());
    } 
}

你的代码布局很差。我的意思是说

  • 您应该声明Board的实例,而不是使用静态变量。Ie
    Board b=新板(0)
  • 不是所有的东西都应该是静态的,因为变量通常应该通过类的实例来访问,除非您使用的是单例
  • 并非所有事情都应该公开。相反,应该使用私有变量,然后通过进行操作(换句话说,您希望签出)
  • System.out.println(…)
    中的
    “”+someInt
    不是必需的
实现了我刚才提到的大部分内容的示例类可能如下所示

public class Test {
    //Private int that can be manipulated via the use of getters and setters.
    private int someInt;

    private Test() {
        someInt = 0;
    }

    //Setter
    public void setSomeInt(int valToSet) {
        someInt = valToSet;
    }

    //Getter
    public int getSomeInt() {
        return someInt;
    }

    public static void main(String[] args) {
        //Instance of Test
        Test t = new Test();

        //Updates the value of the instance
        t.setSomeInt(10);

        //Prints the value from that same instance
        System.out.println(t.getSomeInt());
    }
}

我认为你们的课程需要稍微重新设计一下。
Board
类做得太多了。以下是您可能会考虑的粗略设计:

// Represents a single die
Dice
    int roll()  // Returns 1-6

// A single player. Maintains its own position on board
Player
    int getPosition()
    void move(int numSpaces)
    int bankAccount = 200

// This will contain all the spaces and whatever actions
// happen on those spaces
Board
    ?? getAction(int spaceNumber)

Bank
    payAmount(int amount)
    receiveAmout(int amount)

// The manager class. Maintains the board and players and bank
Game
    Dice dice1, dice2
    Player players[4]
    Board board
    Bank bank
    void nextTurn()
        int roll1, roll2;
        do {
            roll1 = dice1.roll()
            roll2 = dice2.roll()
            players[currentPlayer].move(roll1 + roll2)
            board.getAction(players[currentPlayer].getPosition()]
        } while roll1 == roll2
        currentPlayer = (currentPlayer + 1) % 4

没有
静态
变量。
游戏
类管理所有其他类之间的交互。

缩小错误范围的一种方法是回顾类的结构,问问自己如何定义它们(因为它们可能比需要的更复杂),并确保您已经简化了他们的每个需求:

  • “骰子”的掷骰次数是否有限
通常一个游戏决定你掷骰子的次数,以及你一次需要多少骰子,所以一个roll()函数就足够了,骰子的数量由使用它的类决定

  • 一个骰子是否记得它过去掷过的骰子,以便你以后可以问它曾经掷过什么?(换句话说,你会掷骰子,用骰子执行其他动作,甚至是玩游戏的其他部分,然后回来看看骰子掷了什么?可能不会)
骰子提供的价值立即被游戏使用,并被游戏中的某些动作存储或使用,而不是骰子本身

  • 如果您创建了100个骰子实例,每个实例是否都有自己的掷骰值,或者您是否可以掷骰子24并期望在骰子81上显示相同的值
关于在全局/静态变量中存储成员值的提示

或者简单地说,使用以下方法是否不能实现Dice类的目标

public getRoll() {
    return number.nextInt(6) + 1;
}
public boolean compareValues(int roll1, int roll2) {
    return roll1 == roll2;
}
附加问题:什么是骰子旗

接下来让我们看一下Board类:您要做的第一件事是将值或Dice.roll1和Dice.roll2存储在属于Board的变量中,它们是0,因为还没有发生任何其他事情。你现在可以对骰子类做任何你想做的事情,这两个属于棋盘的值不会受到影响。您可以按照与上面类似的方式分解所有预期功能:

  • 一个棋盘的实例是否只得到2个骰子卷
可能不会,所以不要使用静态变量

  • 是否只有一个通用板,而游戏不能在不退出游戏并重新启动它的情况下使用另一个?(否则,所有这些掷骰值仍然存在)
可能不会,您应该创建一个Board的实例

  • 更多的东西,你可以进一步解剖
最后是斑点鱼。。。看起来你指的是通用棋盘,但创建了一个与棋盘无关的骰子实例,滚动它们并询问棋盘上的玩家现在在哪里,因为你使用的是不同的骰子,所以仍然是他们开始的地方

将现实生活中发生的所有事情都写成简短的句子,然后慢慢地将其转换成Java,即游戏开始:每个玩家掷两个骰子,如果掷骰子相同,请说“耶”,然后将玩家移动两个骰子的组合距离

变成:

// create the game, and a Dice for it
Board myGame = new Board();
Dice dice = new Dice();
int player = 0;

// roll the dice twice
int roll1 = dice.roll();
int roll2 = dice.roll();

// celebrate if the rolls double up
if (roll1 == roll2) {
    System.out.println("yay");
}
int distanceToMove = roll1 + roll2;

// move the player
myGame.movePlayer(player, distanceToMove);

无论何时遇到困难,都要简化

首先,这些不应该是静态的。你有一块棋盘的实例和一对骰子。你为什么要制造这些静电?将它们关联到实例。你不希望世界上所有的棋盘或所有的骰子都有相同的值,是吗?你如何将值从一个类传递到另一个类?还有,你在代码中的什么地方传递值?没有;)board中的roll1与骰子中的roll1不同(即使它们是静态的,并且被称为相同的-它们与您声明它们的类相关)。您应该执行类似board.roll1=dice.roll1的操作。滚完之后。这个静态int roll1=Dice.roll1;终身不连接这些变量是静态的,因为每当我将它们设置为非静态时,主函数将无法正常运行。那么,我到底需要做什么才能使骰子中的roll1和roll2转移到棋盘中的roll1和roll2,因此,我可以计算用户的当前位置?因此,如果我这样做,并创建一个Board类的新实例,使所有内容都保持静态,那么我需要在构造函数中定义什么?如果需要,您可以使用默认构造函数。这取决于实例化
Board
类时您试图实现的目标
public getRoll() {
    return number.nextInt(6) + 1;
}
public boolean compareValues(int roll1, int roll2) {
    return roll1 == roll2;
}
// create the game, and a Dice for it
Board myGame = new Board();
Dice dice = new Dice();
int player = 0;

// roll the dice twice
int roll1 = dice.roll();
int roll2 = dice.roll();

// celebrate if the rolls double up
if (roll1 == roll2) {
    System.out.println("yay");
}
int distanceToMove = roll1 + roll2;

// move the player
myGame.movePlayer(player, distanceToMove);