简体   繁体   English

如何从另一个类中获取字符串?

[英]How to get a string from another class?

Searched everywhere trying to find how to do this. 搜索到处试图找到如何做到这一点。 I want to get a string from one class that i set the variable before going into the second class and allow me to use it as a string in this class. 我想从一个类中获取一个字符串,我在进入第二个类之前设置变量,并允许我在此类中将其用作字符串。

Basically there's a String called LastName that i want to use in another class. 基本上有一个名为LastName的String,我想在另一个类中使用。

Here's my code if needed. 如果需要,这是我的代码。

First Login Class: 首次登录类:

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();
    }

}
}

Second Agent Class: 第二代理类:

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();
}
}

Any help is incredibly appreciated. 任何帮助都非常感激。 Thanks 谢谢

I can see you are probably quite new to Java - so you may be lacking a few fundamentals. 我可以看到你可能对Java很陌生 - 所以你可能缺少一些基础知识。

Probably best to just pass the string in as a parameter to the method in your case eg 可能最好只将字符串作为参数传递给您的方法中的方法,例如

public void WelcomeAgent(String lastName) {

and when you call the method, send that value in eg 当你调用方法时,在例如发送该值

AgentObject.WelcomeAgent(userinput);    

Btw, a few convention tips. 顺便说一句,一些常规技巧。 Variables and functions should start with a lower case . 变量和函数应以小写字母开头。 eg 例如

String lastName;

and

Citizen citizen = new Citizen();

Do you want to access the value of LastName in class Agent ? 是否要在类Agent中访问LastName的值?

What if you change it like this 如果你这样改变怎么办?

public class Login {

 String FirstName, LastName;



public void LoginScreen() {

Agent AgentObject = new Agent();
Citizen CitizenObject = new Citizen();
....
LastName = input.next();
 ...
 }

 public String getLastName()
 { return LastName; }

and in class Agent 并在类Agent中

you can use LoginObject.getLastName() to get the it. 您可以使用LoginObject.getLastName()来获取它。

Write a getter method for the lastName in your Login class. Login类中为lastName写一个getter方法。 After setting the value in the Login class send the reference of this class object to the other classes where you want to use. Login类中设置值之后,将此类对象的引用发送到您要使用的其他类。 Finally call the onject.getLastName() method and use it. 最后调用onject.getLastName()方法并使用它。

Example: 例:

Suppose Login class and Agent class and lastName was declared in Login class then pass the reference of the Login class to the Agent class and access it from the Agent class using the getter method as I have stated above. 假设在Login类中声明了Login类和Agent类以及lastName ,然后将Login类的引用传递给Agent类,并使用上面所述的getter方法从Agent类访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM