实例变量的值在两个类中是不同的。为什么? (+更多问题) [英] values of an instance variable is different in two classes. Why? (+ more questions)
问题描述
我有一个 TurnIndicator
类,它的方法 getTurn();
只是返回String的 BLACK,BLACK,然后在WHITE和BLACK之间交替。
I have a TurnIndicator
class, and its method getTurn();
just returns String of "BLACK", "BLACK" and then alternates between "WHITE" and "BLACK".
在我的板
类中,它结合了包中的所有类并使其一起工作,我创建了一个 TurnIndicator
class, _turn
,并声明一个具有初始值 _t = _turn的实例变量
_t
。 getTurn();
然后,我为 _t定义了一个访问器方法
- getT()
。
Inside my Board
class, which combines all classes in the package and make it work together, I created an instance of TurnIndicator
class, _turn
, and declared an instance variable _t
with initial value _t = _turn.getTurn();
Then, I defined an accessor method for _t
- getT()
.
现在,当我在板内打印
类,它打印黑,黑等等。 _t
但是,当我在另一个类中使用 getT()
访问 _t
值时, SelectPieceListener
,这将打印 null
。
Now, when I print the _t
value inside the Board
class, it prints "BLACK", "BLACK", and so on.
However, when I access _t
value using getT()
in another class, SelectPieceListener
, this prints null
.
为什么是这样吗?
我附上下面的类定义。不相关的部分被删除。
I attach my class definitions below. Irrelevant parts are removed.
我最近要做的是取决于 c == _st.checkMoveImpossible()
条件是让游戏玩家选择两种选择 -
What I want to do eventually is depending on c == _st.checkMoveImpossible()
condition, to make the game player choose between two options -
1。点击选择按钮(在这种情况下, _cmi
成为 1 和 selectClicked()
方法为 if(_cmi == 1)
block。更具体地说,它改变了 _t
值的顺序,以便玩家跳过一圈)或
1. by clicking select button(in this case, _cmi
becomes 1, and selectClicked()
method do desired action for if(_cmi == 1)
block. More specifically, it changes order of _t
value, so that the player skips one turn), or
2。通过点击滚动按钮(在这种情况下, _cmi
成为 2 ,并在 scrollClicked()
方法中执行类似的操作,让当前玩家移动另一个玩家的作品。)
2. by clicking scroll button (in this case, _cmi
becomes 2, and do similar thing in scrollClicked()
method to let current player move the other player's piece.).
我只是把 _cmi == 1
案例动作工作,把所有内容放在 selectClicked()
方法,但是我必须提供 _cmi == 2
case(当玩家点击滚动按钮),所以我不得不将其与方法分离。
I made just the _cmi == 1
case action work, by putting everything inside selectClicked()
method, but I had to provide _cmi==2
case(when the player clicked scroll button), so I had to decouple it from the method.
现在,我一直试图使它在 SelectPieceListener
class,所以如果我成功,我可以稍后将它移动到 Board
类。
Now, I have been trying to make it work in SelectPieceListener
class for now, so that I could move it to the Board
class later if I succeed.
然而我做了几个小时的尝试都失败了。
我一次编码了一个或两个,这是我第一次使用很多类进行项目。我真的在这种事情上没有经验。
However, every attempt I made for hours failed. I have coded one class or two at a time, and this is my first time doing projects with many classes. I am really unexperienced at this kind of things.
如果你能给我一些建议,那将有助于我。
If you could give me some advice, that will help me a lot.
我知道这是非常繁琐的工作,但我可能无法自己做这个。
请花一些时间阅读代码并帮助我。
I know this is really tedious work, but I may not be able to do this by myself.. Please spend some time to read the code and help me.
public class Board {
private Structure _st;
private TurnIndicator _turn;
private String _t;
private int _cmi;
public Board() {
_turn = new TurnIndicator();
_t = _turn.getTurn();
_cmi = 0;
}
public ScrollListener getScroll(){
return _scroll;
}
public String getT() {
return _t;
}
public int setcmi(int cmi) {
return _cmi = cmi;
}
public void selectClicked(char dir) {
int index = _scroll.getX();
HNode<ArrayList<String>> current = _st.getPosition(index);
System.out.println("checkMoveImpossible() = " + _st.checkMoveImpossible());
System.out.println("_t in Board = " + _t);
if(!current.getDatum().isEmpty()) {
if(_t == current.getDatum().get(current.getDatum().size()-1) && _cmi == 0) {
if(0 <= index && index < 17 && index != _st.indexOfSpBack()) {
current.forward(_t, current.getDatum().size());
}
else if(17 <= index && index < 25) {
char direction = dir;
_st.start(current, direction);
}
else if(index == _st.indexOfSpBack()) {
current.backward(_t, current.getDatum().size()-1);
}
_t = _turn.getTurn();
System.out.println("turn is "+ _t);
display();
}
else if(_t != current.getDatum().get(current.getDatum().size()-1) && _cmi == 0) {
String s = _t == "WHITE" ? "BLACK" : "WHITE";
System.out.println("It's "+_t+"'s turn now and only "
+s+" can move this piece.");
}
}
if(_cmi == 1) {
_t = _turn.getTurn();
_cmi = 0;
System.out.println("_cmi if block is reached");
display();
}
update();
}
下面是SelectPieceListener类定义
below is the SelectPieceListener class definition
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SelectPieceListener implements ActionListener {
private Board _board;
private Structure _st;
private String _t;
public SelectPieceListener(Board board) {
_board = board;
_st = _board.getStructure();
_t = _board.getT();
}
@Override
public void actionPerformed(ActionEvent e) {
char c = _t == "WHITE" ? 'b' : 'w';
_board.selectClicked('l');
System.out.println(_t);
if(c == _st.checkMoveImpossible()) {
String theOther = _t == "WHITE" ? "BLACK" : "WHITE";
System.out.println("No possible move for "+_t+": click select to skip turn, " +
"or click scroll to move " + theOther + " pieces.");
_board.setcmi(1);
_board.update();
_board.selectClicked('l');
}
}
}
推荐答案
嗯,
我看到你有一些我不推荐的东西。 SelectPieceListener中的_t和_st字段不是必需的,因为您可以从_board字段获取它们,为什么不需要重复引用?
I see you have some things I wouldn't recommend. The _t and _st fields in SelectPieceListener are not necessary as you can get them from the _board field, why keep duplicated references if it's not necessary?
另一方面,我看到你用==做很多String比较,你应该总是使用STRING_LITERAL.equals(variable)。
On the other hand, I see you do a lot of String comparisions with ==, you should always use "STRING_LITERAL".equals(variable).
之后,我建议您使用像Eclipse这样的IDE,以便您可以调试部分代码。如果您的变量为空,那么在某个时候可能会丢失,调试器将是找到问题的最佳方式。
After that, I would recommend you to use an IDE like Eclipse so you can debug parts of your code. If your variable is null, it's probably getting lost at some point, a debugger will be the best way to find the issue.
这篇关于实例变量的值在两个类中是不同的。为什么? (+更多问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!