所有JFrames對於我的PAT上學方法編輯它,我做我的默認構造函數和這裏看到參數化的構造函數:默認/參數化的構造函數=如何在包裝
public class Client
{
//attributes
private String clientName;
private String clientPass;
private double currentBal;
private double savingsBal;
private boolean verify;
//default constructor
public Client()
{
clientName = "";
clientPass = "";
currentBal = 0.0;
savingsBal = 0.0;
verify = false;
}
//parameterised constructor
private Client(String username,String password,int accNum,
double curBal,double savBal, boolean ver)
{
clientName = username;
clientPass = password;
currentBal = curBal;
savingsBal = savBal;
verify = ver;
}
的問題是,我有在此之後創建了幾個方法,爲了讓它們正常工作,它們需要更改我的構造函數中爲包中的所有不同表單分配的值。 不幸的是,這似乎並不奏效。無論我做什麼,變量只是暫時改變。 我錯過了什麼嗎?有什麼我應該在我的構造函數中完成不同的工作,還是需要在我的方法中做一些額外的工作才能更改。
所有幫助非常感謝。 :)
編輯: 這裏是我的方法之一:
public void newClient(String username,String password) throws IOException
{
//Sets username and password to user input and assigns the Client
account balances.
clientName = username;
clientPass = password;
savingsBal = (int)((Math.random()*100000000))/100.0;
currentBal = (int)((Math.random()*100000000))/100.0;
//Calls BufferedWriter to write a line to the text file with all
user information.
BufferedWriter bw = new BufferedWriter (new
FileWriter("ClientDatabase.txt",true));
bw.write(clientName+"_"+clientPass+"_"+currentBal+"_"+savingsBal);
bw.newLine();
//Closes BufferedWriter
bw.close();
}
//Verifies new Client via their username and password.
這工作正常,並且它是所有寫入文本文件。但是,當我嘗試在我的任何其他JFrames中使用這些值中的任何一個值時,似乎它們已被重置爲0或空值...
你不在示例中引用'Client'類的實例。 'newClient'是什麼類的方法的一員?可能不是「客戶」。你只能在這個類中使用像這樣的類變量。在你需要引用的地方(比如'Client c = new Client()'),然後你可以改變那些實例成員(比如'c.setName(aName)') – Fildor