到处搜索试图找到如何做到这一点。我想从一个类中获取一个字符串,我在进入第二个类之前设置了变量,并允许我在这个类中将它用作字符串。
基本上有一个名为 LastName 的字符串,我想在另一个类中使用它。
如果需要,这是我的代码。
首次登录类:
import java.util.Scanner;
public class Login {
public void LoginScreen() {
Agent AgentObject = new Agent();
Citizen CitizenObject = new Citizen();
String FirstName;
String LastName;
Scanner input = new Scanner(System.in);
System.out.println("Welcome!");
System.out.print("Please Enter Last Name: ");
LastName = input.next();
System.out.print("Please Enter First Name: ");
FirstName = input.next();
System.out.println("Hello " + FirstName + " " + LastName);
System.out.print("Enter Password: ");
String userinput = input.next();
if (userinput.equals("Timmo")) {
AgentObject.WelcomeAgent();
} else {
CitizenObject.WelcomeCitizen();
}
}
}
第二代理类:
public class Agent {
public void WelcomeAgent() {
Welcome WelcomeObject = new Welcome();
Login LoginObject = new Login();
System.out.println("Access Granted");
System.out.print("Loading Data ");
int progress = 0;
while (progress <= 100) {
System.out.print(progress + " ");
progress++;
}
System.out.println(" Data Loaded");
System.out.println("Welcome Agent " + LastName);
LoginObject.LoginScreen();
}
}
非常感谢任何帮助。谢谢