美好的一天!
我创建了两个类,即设置和游戏;在我的游戏中首先访问Setting类。
在我的设置类中,我从Game调用setter方法.setDifficulty.
并为其赋值,例如== 2.
public class Setting extends javax.swing.JDialog {
public Setting (JFrame owner) {
super(owner, true);
initComponents();
setSize(400, 250);
setLocation(370, 250);
getContentPane().setBackground(new Color(128, 201, 20));
}
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
MainGame m2 = new MainGame(this);
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
然后我访问我的第二个CLass,即游戏。但是我无法在setter方法之外获得hardLvl的值。 (参见我对代码的评论)
public class Game extends javax.swing.JDialog {
private int difficultLvl = 0;
public Game(JFrame owner) {
super(owner, true);
initComponents();
setSize(500, 500);
setLocation(300, 120);
getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
System.out.println(difficultLvl); //SHOULD BE == 2, but == 0;
}
public void setDifficulty(int Difficulty) {
this.difficultLvl = Difficulty;
System.out.println(difficultLvl); == to 2 which is correct...
}
问题是我无法访问setter类之外的difficultyLvl值...它返回默认的赋值,在这种情况下为0.我做错了什么?如何访问setter方法中的值。我使用this.difficultLvl
但没有结果。我是java的新手...请帮忙!非常感谢您的帮助。
谢谢。
答案 0 :(得分:3)
在游戏的构造函数中,'difficultLvl'成员将为零,因为它是初始化的 - 没有理由期望它是2.一旦构造,你使用setter方法将值设置为2;从那时起,该值将为2,直到设置为其他值。
如果你要添加一个getter方法:
public int getDifficulty() {
return difficultLvl;
}
并打电话给你,你会看到价值。
我怀疑你不想在每次点击鼠标时构建一个新游戏,而是保留一个游戏,只需在鼠标点击时调用setter方法:
private MainGame m2 = new MainGame(this);
public Setting (JFrame owner) {
super(owner, true);
initComponents();
setSize(400, 250);
setLocation(370, 250);
getContentPane().setBackground(new Color(128, 201, 20));
}
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
答案 1 :(得分:2)
difficultLvl
是一个实例变量,因此每个实例都有一个值。每次创建Game
的新实例时,它都会将自己的diffucultLvl
初始化为0
。如果您将difficultLvl
设置为Game
,则不会更改其他Game
个实例,并且不会影响将来的新Game
实例。
private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
MainGame m2 = new MainGame(this);
m2.setDifficulty(jComboBox1.getSelectedIndex());
}
在此代码中,您创建了一个游戏MainGame m2 = new MainGame()
,但它有默认难度,即构造函数中打印的内容。接下来,您设置其难度级别(如果您在此之后打印难度,它将是正确的)。然后游戏被抛弃:它超出了范围 - 它只是一个局部变量。
答案 2 :(得分:1)
您正在构造函数中打印值。此时,该值将为0.仅在调用setDifficulty()之后,该值才设置为2.
答案 3 :(得分:1)
这是因为您首先创建了MainGame对象,而System.out.println位于构造函数中。 然后,您调用setter来更改值。 但是构造函数已经打印了初始值(因为它是第一个)。
Sollution:extendLevel需要是构造函数的一个参数才能使其工作。
使用调试器并仔细查看。这是一个非常基本的事情,因此充分了解这里发生的事情非常重要。
答案 4 :(得分:1)
我看到了一些问题。
首先您确定要在MainGame
课程中实例化Setter
吗?它是Game
的子类还是其他不同的子类?如果代码正确,difficultLvl' in
MainGame has nothing to do with
difficultyLvl in
Game` - 两者都是不同的类。
第二如果您想要游戏的难度级别,可以使用构造函数执行:
public Game(int difficultyLevel) {
this.difficultyLvl = difficultyLevel;
}
或使用setter方法,但是在创建对象之后设置值,因为我们都无法展望未来,所以除了初始值之外你什么都看不到实际代码。
答案 5 :(得分:1)
这一行将调用构造函数并创建一个difficultLvl
= 0;
MainGame m2 = new MainGame(this);
打电话后
m2.setDifficulty(jComboBox1.getSelectedIndex());
然后
difficultLvl
的{{1}}将设置为所选索引。
答案 6 :(得分:1)
您似乎在鼠标单击操作处理程序中创建了一个MainGame实例,该处理程序在方法调用完成后立即收集垃圾。因此,您的值(= 2)将丢失,因为收集包含它的对象。因此,在另一次单击时,您将创建一个具有值(= 0)的新实例,因为您使用0初始化它。
private int difficultyLvl = 0;
我不太了解你想做什么,但似乎你想要在你的应用程序的某个地方保持指向游戏对象的句柄。