简体   繁体   English

将值从一个类的静态变量保存到另一个Java

[英]Save value from a static variable from a class to another java

I have a problem .When i try to store a global public static value which i try to acces from another class. 我有一个问题。当我尝试存储一个全局公共静态值时,尝试从另一个类访问。

In class Move i have the function from button event. 在“移动”类中,我具有按钮事件的功能。

import static monopoly.Interface.zar1;
import static monopoly.Interface.zar2;


public class Move {

public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0;  //after start
public int untilstart=0;  //until start
public int locbefore=0; //save location before passing over start

public void End(java.awt.event.ActionEvent evt) {                                    

}                                   

public void RollAction(java.awt.event.ActionEvent evt) {                                     

    Dice dice = new Dice();   

    rolezar1 = dice.RollDice();
    rolezar2 = dice.RollDice();
    zar1.setText(Integer.toString(rolezar1));
    zar2.setText(Integer.toString(rolezar2));

    Integer pfinal = rolezar2 + rolezar1;

    int[] posx = new int[]{95, 145, 195, 245, 295, 345, 395, 445, 495, 495, 495, 495, 495, 495, 495, 495, 495, 495, 445, 395, 345, 295, 245, 195, 145,  95,  45,  45,  45,  45,  45,  45,  45,  45, 45, 45};
    int[] posy = new int[]{60,  60,  60,  60,  60,  60,  60,  60,  60, 110, 160, 210, 260, 310, 360, 410, 460, 510, 510, 510, 510, 510, 510, 510, 510, 510, 510, 460, 410, 360,310, 260, 210, 160, 110, 60};

    int i = 0;

    int loc = rolezar1 + rolezar2;
    while(i <loc){
        int j=0;
        if (i+location==posx.length){
            untilstart=posx.length-location;
            overstart=loc - untilstart;
            locbefore=location;
            location=0;
        }
        else{
            overstart=0;
            untilstart=0;
        }
        while(j<untilstart){
            Interface.Player1.setBounds(posx[locbefore+j], posy[locbefore+j], 20, 20);
            j++;
            i++;
        }
        j=0;
        while(j<overstart){
            Interface.Player1.setBounds(posx[location+j], posy[location+j], 20, 20);
            j++;
            i++;

        }
        if(overstart==0&&untilstart==0){
            Interface.Player1.setBounds(posx[location+i], posy[location+i], 20, 20);
            i++;
        }

    }
    location+=overstart;

}                                    


public void move(){

}

}

when i call the function in Main class the variable does not retain it's latest value 当我在Main类中调用函数时,变量不保留其最新值

   private void RollActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Move apel = new Move();

    apel.RollAction(evt);
   }

Now the question: How can I use the variable from class Move in Main class correctly ?. 现在的问题是:如何正确使用Main类中Move类的变量? When i put this code in Main class it's work. 当我将此代码放在Main类中时,它起作用了。

I don't find any static members in your Move class . 我在您的Move类中找不到任何静态成员。 only class attribute variable exists . 仅存在类属性变量。 For your program to run as expected 使您的程序按预期运行

Make Dice as Move member, Not method local i,e 将Dice设为Move成员,而不是将方法设为本地i,e

cut Dice dice = new Dice(); Dice dice = new Dice();

and past it along with other class member like this and try 并与其他班级成员一起过去并尝试

public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0;  //after start
public int untilstart=0;  //until start
public int locbefore=0
Dice dice = new Dice()  // Now this may work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM