简体   繁体   English

如何在Java中将字符串值一类调用到另一类

[英]How to call String value one class to another class in Java

public class Sample1 {
    public String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public static void main(String[] args) {
        Sample1 s1= new  Sample1();
        s1.setName("Abc");
    }
}


public class Sample2 {
    public static void main(String[] args) {
        Sample1 n2= new  Sample1();
        System.out.println(n2.getName());
    }
}

I have two classes Sample1 and Sample2 two. 我有两个类Sample1和Sample2两个。 I am allocating string value using setter method and returning in another class by using getter method, but an output is null. 我正在使用setter方法分配字符串值,并使用getter方法返回另一个类,但输出为null。 Why null is an output and how to get string value from one call to another class? 为什么输出为null以及如何从一个调用到另一个类中获取字符串值?

I think you misunderstood the main method, maybe I am wrong, however only one main method is executed. 我认为您误解了main方法,也许我错了,但是只执行了一种main方法。

If you run Sample2.main - on Sample1 you are not setting a name so it is null (Sample1.main is never executed). 如果您运行Sample2.main-在Sample1上没有设置名称,则该名称为null(Sample1.main从未执行)。

If you run Sample1.main - Sample1 is created and assigned a name. 如果运行Sample1.main-将创建Sample1并为其分配一个名称。

So either assign the name in the Sample2.main: 因此,可以在Sample2.main中分配名称:

public static void main(String[] args) {
  Sample1 n2= new  Sample1();
  n2.setName("xxx");
  System.out.println(n2.getName());
}

or do it via constuctor. 或通过构造器完成。

public class Sample1 {
  private final String name;
  public String getName() {
    return name;
  }
  public Sample1(String name) {
    this.name = name;
  }
}

Consider the code : 考虑代码:

Sample1 n2= new  Sample1();
System.out.println(n2.getName());

Here the Name is not set , So you need to set the name before getting the name. 这里没有设置名称,因此您需要先设置名称,然后再获取名称。

Sample1 n2= new  Sample1();
n2.setName("name goes here");
System.out.println(n2.getName());

Also, you can try parameterized constructor in the Sample1 class and access like in sample2 class: 另外,您可以在Sample1类中尝试使用参数化的构造函数,并像在sample2类中那样进行访问:

Sample1 n2= new  Sample1("your name goes here");
System.out.println(n2.getName());

The constructor will be : 构造函数将是:

public Sample2(String name){
this.name = n;
}

3 thing you can add method in Sample1 class and access it in Sample2 class. 您可以在Sample1类中添加方法并在Sample2类中访问它的三件事。

I don't want to set String value in Sample2 class, need to assign string value in Sample1 only, after that i need that string value in Sample2 class 我不想在Sample2类中设置字符串值,仅需要在Sample1中分配字符串值,之后我就需要在Sample2类中使用该字符串值

    public class Sample1 {
    private String _name;
    public String getName() {
        return _name;
    }
    private setName(String name) {
        _name = name;
    }
    public SetNameHelper(){
       setName("somestring");//You will be setting the name in Sample 1
    }
}


public class Sample2 {
    public static void main(String[] args) {
        Sample1 n2= new  Sample1();
        n2.SetNameHelper();
        System.out.println(n2.getName());//You will be getting Name in Sample 2 class
    }
}
class Sample2 {

Sample1 sample1;

Sample2(Sample1 sample){
    this.sample1 = sample;
}

private String getSample1Name() {
    return this.sample1.getName();
}

public static void main(String[] args) {

    Sample1 sample1 = new Sample1();
    sample1.setName("Abc");

    Sample2 n2= new  Sample2(sample1);
    System.out.println(n2.getSample1Name());
}

} }

I think I understand you confusion: you mistake a main function for a constructor ! 我想我理解您的困惑:您将主要功能误认为构造函数

I noticed that you created a main function in each class, whose only role is to create an instance and set the internal fields of the class. 我注意到您在每个类中创建了一个main函数,其唯一作用是创建实例并设置该类的内部字段。 It's probably a Constructor you were looking for. 它可能是您正在寻找的构造函数。

Here's the deal: 这是交易:

  • A main method is the entry point of a program. 主要方法是程序的入口点。 It just stays, to run me: begin executing this code. 运行我,它就一直存在:开始执行代码。 You probably (99.9% of the case) need only one main method per project. 您可能(99.9%的情况)每个项目仅需要一种main方法。
  • A Constructor is a method which creates an instance of, each class, ie an object you can manipulate elsewhere in your code. 构造函数是一种创建每个类的实例的方法,即可以在代码中其他位置操作的对象。 Read up on Object-Oriented Programming 阅读有关面向对象的编程的信息

So here is your example, fixed (I believe), in a way that can bring sample1 value into an sample2 , as you say: 所以这是您的示例,已固定(我认为),以某种方式可以将sample1的值带入sample2 ,如您所说:

public class Sample1 {

    public String name;
    public String Sample1(String initialName){
         this.name = initialName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class Sample2{
    public String name;
    public String Sample2(String initialName){
         this.name = initialName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

public class ProgramEntryPoint{
    public static void main(String[] args) {
        Sample1 s1 = new  Sample1("a name");
        System.out.println("Initial sample1 name: " + s1.getName());
        s1.setName("a New Name!");
        System.out.println("New sample1 name: " + s1.getName());

        Sample2 s2 = new Sample2(s1.getName());
        System.out.println("Initial sample2 name: " + s2.getName());
    }
}

Which, once you run ProgramEntryPoint.main, will print: 一旦运行ProgramEntryPoint.main,它将打印:

Initial sample1 name: a name 初始样本1名称:名称
New sample1 name: a New Name! 新的sample1名称:新名称!
Initial sample2 name: a New Name! sample2的初始名称:新名称!


This is all simple Java stuff, you should read up a few basic tutorials (the ones on oracle website are a good start) 这都是简单的Java东西,您应该阅读一些基本教程(oracle网站上的教程是一个好的开始)

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

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