簡體   English   中英

Java Getter和Setter問題

[英]Java Getter and Setter Problem

美好的一天!

我創建了兩個類,即設置和游戲; 在我的游戲中首先訪問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的新手...請幫忙! 非常感謝您的幫助。 謝謝。

在游戲的構造函數中,'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());
    }   

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() ,但它有默認難度,即構造函數中打印的內容。 接下來,您設置其難度級別(如果您在此之后打印難度,它將是正確的)。 然后游戲被拋棄:它超出了范圍 - 它只是一個局部變量。

您正在構造函數中打印值。 此時,該值將為0.僅在調用setDifficulty()之后,該值才設置為2。

這是因為您首先創建了MainGame對象,System.out.println位於構造函數中。 然后,您調用setter來更改值。 但構造函數已經打印了初始值(因為它是第一個)。

Sollution:難度級別需要是構造函數的參數才能使其工作。

使用調試器並仔細查看。 這是一個非常基本的事情,因此充分了解這里發生的事情非常重要。

我看到了幾個問題。

首先 ,您確定要在Setter類中實例化MainGame嗎? 它是Game的子類還是其他不同的東西? 如果代碼是正確的, difficultLvl' in MainGame has nothing to do with difficultLvl in Game` -都是differen類。

其次,如果你想要游戲的難度級別,可以使用構造函數:

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

或者使用setter方法,但是創建對象之后設置了值並且因為我們都無法展望未來,所以除了實際代碼的初始值之外什么都不會看到。

該行將調用構造函數並使用difficultLvl = 0創建一個對象;

MainGame m2 = new MainGame(this);

你打電話之后

m2.setDifficulty(jComboBox1.getSelectedIndex());

然后difficultLvlm2將被設置到所選擇的索引。

您似乎在鼠標單擊操作處理程序中創建了一個MainGame實例,該處理程序在方法調用完成后立即收集垃圾。 因此,您的值(= 2)將丟失,因為收集包含它的對象。 因此,在另一次單擊時,您將創建一個具有值(= 0)的新實例,因為您使用0初始化它。

private int difficultyLvl = 0;

我不太了解你想做什么,但似乎你想要在你的應用程序的某個地方保持指向游戲對象的句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM