![](/img/trans.png)
[英]In Android Does multipart entity is used for download file too as I can't see any example or details description for that class
[英]How can I use the Strings values stored in a main class in my secondary class? (see description for details. Java)
我得到了一個MainClassTestPrompts類, 該類使用輸入框詢問用戶的用戶名和密碼,然后將每個值存儲在單獨的字符串中。
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MainClassTestPrompts {
public static void main(String[] args) {
JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] DBInputBox = { "Username:", username, "Password:", password };
try {
// I. Username and Password Prompts
JOptionPane.showConfirmDialog(null, DBInputBox, "Login", JOptionPane.OK_CANCEL_OPTION);
String Username = username.getText();
String Password = password.getText();
System.out.println("Username: " + Username);
System.out.println("Password: " + Password);
} catch (Exception e) {
System.out.println(e);
}
}
}
然后,我有另一個類SecondaryClass ,它將執行一組操作,然后將需要存儲的字符串: Username和Password以執行操作。
我的問題是我不知道如何只獲取主類中存儲的字符串值,然后在輔助類中使用它們而又不會再次得到提示。 我想運行我的主類,獲取用戶/密碼,然后最終運行第二個類,該類將選擇存儲的值。
我在第二堂課上嘗試過的
public class SecondaryClassUseCredentials extends MainClassTestPrompts {
public static void main(String[] args) {
MainClassTestPrompts.main(null); // this will just run the MainClassTestPrompt which is not what I want.
String Username = MainClassTestPrompts.Username; //I know this is wrong but is sort of what I'm looking for...
String Password = MainClassTestPrompts.Password;
}
我將假設您對Java還是很陌生,也許對編程而言,否則很難解釋您的設計決策。
首先,您有一個正確的設計原則:一個類應該做一件事。 因此,您的MainClassTestPrompts
會提示您輸入用戶名和密碼,而SecondaryClassUseCredentials
希望對它們進行處理。 這稱為關注點分離。
錯誤是在兩者中都使用main
,或者使用沒有返回類型的方法。 由於將main
用作應用程序的切入點,因此最好不要在這兩個類中都使用main
-它們是業務邏輯的一部分,應用程序接線應該是另一個問題。
因此,您可以做的是定義一個類/方法對,它要求提供憑據並返回它們,而另一對則調用前者並使用其結果。 一次只執行一個步驟,即使我通常建議這樣做,我也將保留靜態方法並且不引入任何接口:
class MainClassTestPrompts {
static Map.Entry<String, String> askForCredentials() {
JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] DBInputBox = { "Username:", username, "Password:", password };
JOptionPane.showConfirmDialog(null, DBInputBox, "Login", JOptionPane.OK_CANCEL_OPTION);
return new SimpleEntry<>(username.getText(), password.getText());
}
}
現在您的呼叫者類可能如下所示:
class SecondaryClassUseCredentials {
static void doSomething() {
Map.Entry<String, String> credentials = MainClassTestPrompts.askForCredentials();
String username = credentials.getKey();
String password = credentials.getValue();
...
}
}
對於下一次迭代,請嘗試擺脫靜態變量並使用接口。 為了讓您自己嘗試一下,我只會建議SecondaryClassUseCredentials
如下所示:
class SecondaryClassUseCredentials {
private final Supplier<Map.Entry<String, String>> credentialsSupplier;
SecondaryClassUseCredentials(Supplier<Map.Entry<String, String>> credentialsSupplier) {
this.credentialsSupplier = credentialsSupplier;
}
static void doSomething() {
Map.Entry<String, String> credentials = credentialsSupplier.get();
String username = credentials.getKey();
String password = credentials.getValue();
...
}
}
在MainClassTestPrompt中,變量聲明應該在try catch塊之前,然后它將起作用。 在編寫時,變量為String Username; String Password
String Username; String Password
本地的,可以嘗試捕獲,您不能從SecondaryClass訪問它們。
public class MainClassTestPrompts { //Better private String Username; private String Password; public String getUsername(){ return this.Username; } public String getPassword(){ return this.Password; } public static void main(String[] args) { JTextField username = new JTextField(); JTextField password = new JPasswordField(); Object[] DBInputBox = { "Username:", username, "Password:", password}; //Not ideal but will work try { // I. Username and Password Prompts JOptionPane.showConfirmDialog(null, DBInputBox, "Login", JOptionPane.OK_CANCEL_OPTION); //Edited Username = username.getText(); Password = password.getText(); System.out.println("Username: " + Username); System.out.println("Password: " + Password); } catch (Exception e) { System.out.println(e); } } }
然后在中學
public class SecondaryClassUseCredentials extends MainClassTestPrompts { public static void main(String[] args) { MainClassTestPrompts.main(null); // this will just run the MainClassTestPrompt which is not what I want. String Username = MainClassTestPrompts.getUsername(); //I know this is wrong but is sort of what I'm looking for... String Password = MainClassTestPrompts.getPassword(); }
如果在擴展MainClass時只想使用繼承,那么SecondClass的代碼也將起作用。 但是,如果您想在其他任何地方使用密碼和用戶名,則最好編寫適當的get()方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.