Java Getter 和 Setter 问题

发布于 2024-10-15 19:40:53 字数 1579 浏览 3 评论 0原文

再会!

我创建了两个类,即设置和游戏;在我的游戏中,首先访问设置类。

在我的设置类中,我从 Game 中调用 setter 方法,即 .setDifficulty. 并为其分配一个值,example == 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());
        }           

然后我访问我的第二个类,即游戏。但我无法在setter方法之外获取困难Lvl的值。 (请参阅我对代码的评论)

     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 类之外的困难Lvl 值...它返回到默认分配的值,在本例中为 0。我做错了什么?如何访问setter方法中的值。我使用了 this.difficultLvl 但没有结果。我是java新手...请帮忙!我们将非常感谢您的帮助。 谢谢。

Good day!

I created two classes namely Setting and Game; In my game access the Setting class first.

In my setting class, I call the setter method from Game which is .setDifficulty. and assign a value to it, example == 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());
        }           

Then I access My second CLass which is the game. But I cannot get the value of the difficultLvl outside the setter method. (See my comments on the code)

     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...
        }

The problem is that I cannot access the difficultLvl value outside the setter class... It returns to its default assigned value which on this case is 0. What am I doing wrong? How can access the value inside the setter method. I used this.difficultLvl but with no result. I am just new in java... Please help! Your help would be highly appreciated.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

轻拂→两袖风尘 2024-10-22 19:40:53

在游戏的构造函数中,“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());
    }   

Within the constructor of game the 'difficultLvl' member will be zero as that is what it is initialised to - no reason to expect it to be 2. Once constructed you use the setter method to set the value to 2; from then on the value will be 2 until set to something else.

If you were to add a getter method:

public int getDifficulty() {
    return difficultLvl;
}

and call this you'll see the value.

I suspect you don't want to construct a new Game on every mouse click but instead keep one and just call the setter method on mouse click:

   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());
    }   
拔了角的鹿 2024-10-22 19:40:53

difficultLvl 是一个实例变量,因此每个实例都有一个值。每次创建 Game 的新实例时,它都会将自己的 diffucultLvl 初始化为 0。如果您在一个 Game 中设置 difficultLvl,则不会更改其他 Game 实例,也不会影响未来的新 >游戏实例。

private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
        dispose();
        MainGame m2 = new MainGame(this);
        m2.setDifficulty(jComboBox1.getSelectedIndex());
}      

在此代码中,您创建了一个游戏 MainGame m2 = new MainGame(),但它具有默认难度,这是构造函数中打印的内容。接下来,设置其难度级别(如果在此之后打印难度,它将是正确的)。然后游戏就被扔掉了:它超出了范围——它只是一个局部变量。

difficultLvl is an instance variable, so it has a value for each instance. Every time you create a new instance of Game, it has its own diffucultLvl initialized to 0. If you set the difficultLvl in one Game, it doesn't change it for other Game instances and it doesn't affect future new Game instances.

private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {                                   
        dispose();
        MainGame m2 = new MainGame(this);
        m2.setDifficulty(jComboBox1.getSelectedIndex());
}      

In this code, you create a game, MainGame m2 = new MainGame(), but it has default difficulty, which is what is printed in the constructor. Next, you set its difficulty level (if you print the difficulty after this, it will be right). Then game is thrown away: it goes out of scope - it was only a local variable.

笛声青案梦长安 2024-10-22 19:40:53

我看到了几个问题。

首先您确定要在Setter类中实例化MainGame吗?它是 Game 的子类还是不同的东西?如果代码正确,则 difficultLvl' inMainGamedifficultLvlinGame` 无关 - 两者都是不同的类。

第二如果您想要游戏的难度级别,可以使用构造函数:

 public Game(int difficultyLevel) {
   this.difficultyLvl = difficultyLevel;
 }

或使用 setter 方法来实现,但然后您在创建对象之后设置值,并且,因为我们都无法展望未来,所以您除了实际代码的初始值之外什么也看不到。

I see a couple of problems.

First are you sure to instantiate MainGame in the Setter class? Is it a subclass of Game or something different? If the code is correct, difficultLvl' inMainGamehas nothing to do withdifficultLvlinGame` - both are differen classes.

Second if you want the difficulty level for a game, either do it with the constructor:

 public Game(int difficultyLevel) {
   this.difficultyLvl = difficultyLevel;
 }

or with the setter method, but then you set the value after creating the object and, because we all can't look into the future, you'll see nothing but the initial value with your actual code.

假扮的天使 2024-10-22 19:40:53

您正在构造函数中打印该值。此时该值将为 0。只有在调用 setDifficulty() 后该值才会设置为 2。

You are printing the value in the constructor. At that point the value will be 0. Its only after setDifficulty() is called that the value is set to 2.

瞳孔里扚悲伤 2024-10-22 19:40:53

这是因为您首先创建 MainGame 对象,并且 System.out.println 位于构造函数中。
然后调用 setter 来更改值。
但构造函数已经打印了初始值(因为它是第一个)。

解决方案:困难级别需要是构造函数的参数才能起作用。

使用调试器并仔细观察。这是一个非常基本的事情,因此充分理解这里发生的事情很重要。

Its because you are first creating the MainGame object and the System.out.println is in the constructor.
THEN you call the setter to change the value.
But the Constructor already printed the initial value (since it came first).

Sollution: The difficultyLevel needs to be a parameter of the Constructor for this to work.

Use a debugger and take a closer look. This is a very basic thing, so it is important to fully understand what is happening here.

〗斷ホ乔殘χμё〖 2024-10-22 19:40:53

此行将调用构造函数并创建一个 difficultLvl = 0 的对象;

MainGame m2 = new MainGame(this);

然后你打电话

m2.setDifficulty(jComboBox1.getSelectedIndex());

之后
m2difficultLvl 将设置为所选索引。

This line will call the constructor and create an object with difficultLvl = 0;

MainGame m2 = new MainGame(this);

And after you call

m2.setDifficulty(jComboBox1.getSelectedIndex());

then
difficultLvl of m2 will be set to the selected index.

冷情 2024-10-22 19:40:53

您似乎在鼠标单击操作处理程序中创建了一个 MainGame 实例,一旦方法调用完成,该实例就会立即收集垃圾。因此,您的值 (=2) 会丢失,因为包含它的对象已被收集。因此,再次单击时,您将创建一个具有值 (=0) 的新实例,因为您使用 0 对其进行初始化。

private int oddLvl = 0;

我不太了解你想要做什么,但似乎你想在应用程序中的某个地方保留一个指向游戏对象的句柄。

You seem to create an instance of MainGame in your mouse click action handler which in turn get's garbage collected as soon as the method invocation finishes. So your value (=2) is lost since the object containing it is collected. So on another click you create a new instance which has a value (=0) since u initialize it with 0.

private int difficultLvl = 0;

I do not know much about what you want to do but it seems that you want to keep a handle pointing at the game object there somewhere in your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文