2010-11-10 38 views
0

如何在另一個類中使用變量drawMax?Java,使用類中的變量

public int DrawNumberSetting (int drawMax) 
{ 
    System.out.print ("Please enter the maximum number of draws you want to play: "); 
    drawMax = scan.nextInt(); 
    return drawMax; 
} 
+0

你的意思是說,你想在其他類中使用一個本地方法變量?請詳細說明。 – 2010-11-10 10:16:22

+4

你想要做什麼? – 2010-11-10 10:16:45

+0

我只想使用用戶在這個方法中輸入的值,在另一個類中。那可能嗎? – 2010-11-10 10:21:11

回答

1

比方說你drawNumberSetting(INT drawMax)在A級。這意味着,具有A類的一個實例,任何其他類可以調用該方法,並使用返回值聲明。

class B 
{ 
    public void my otherMethod() 
    { 
     A a = new A(); 
     int drawMax = a.drawNumberSetting(5); 
    } 
} 
0

你可以把drawMax到類級變量,提供一個getter \存取方法,就像這樣:

private int drawMax; 

public void DrawNumberSetting() 
{ 
    System.out.print ("Please enter the maximum number of draws you want to play: "); 
    drawMax = scan.nextInt(); 
} 

public int getDrawMax() 
{ 
    return drawMax; 
}