如何将一个字符串变量传递给Java中的新对象

huangapple go评论60阅读模式
英文:

How to pass a String variable to a new object in java

问题

我对Java不太了解,不确定如何正确操作。

我有一个String变量“textMain”,我想将它传递给一个新对象“TextToSpeech”。这可能吗?如果可以,应该如何操作?

不幸的是,我必须在对象外部声明这个变量,但是这个对象似乎“看不到”这个变量。

    String textMain = "文本内容";
    textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                speak(textMain); // textMain不可见
            }
        }
    });

如果我写错了什么地方,还请原谅,我还不太了解正确的术语。
英文:

I am new to Java and and not sure how to do this correctly.

I have a String variable textMain and I would like to pass it into a new object TextToSpeech. Is it possible? If so, how to do it?

I have to declare this variable outside of the object, unfortunately this object does not 'see' this variable.

String textMain = "text text";
textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak(textMain); // textMain doesn't visible
        }
    }
});

Sorry if I wrote something wrong, I don't know the correct nomenclature yet.

答案1

得分: 1

你想传递的对象需要有一个字段来存储这个值。

假设你有一个名为TextToSpeech的类,它的构造函数有一个字符串参数,用于在对象创建时设置值。

public class TextToSpeech {
  private String textMain;
  ...

  public TextToSpeech(String text, ...) {
    textMain = text;
    ...
  }
}

或者你可以在对象创建后使用setter方法来设置值。

public void setText(String text) {
  textMain = text;
}
英文:

Your object you want to pass the string needs to have a field to store the value

Let's say that you have a class TextToSpeech with a constructor that has a string parameter to set the value at object creation.

public class TextToSpeech {
  private String textMain;
  ...

  public TextToSpeech(String text, ...) {
    textMain = text;
    ...
  }
}

Or you can have a setter method in order to set the value after object creation

public void setText(String text) {
  textMain = text;
}

答案2

得分: 1

任何时候在匿名类/lambda中引用局部变量,都需要将该变量声明为final(不可变)。

final String textMain = "文本文本";
textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak(textMain); // textMain不可见
        }
    }
});
英文:

Any time you are referencing a local variable in an anonymous class / lambda you need to declare that variable as final (immutable).

final String textMain = "text text";
textToSpeechSystem = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            speak(textMain); // textMain doesn't visible
        }
    }
});

答案3

得分: -1

因为TextToSpeech.OnInitListenertextMain在内存中的位置不同:TextToSpeech.OnInitListener被定位在中,在当前上下文关闭后仍然可用,但是textMain被定位在中,在当前上下文关闭后不再可用。

要修复这个问题,你只需要将textMain移动到中。

final String textMain = "文本内容";
英文:

Thats because TextToSpeech.OnInitListener and textMain has different location in memory: TextToSpeech.OnInitListener is been located in the heap and available after current context will be closed, but textMain is been located in the stack and not available after current context will be closed.

To fix it. all you have to do is to move textMain to the heap.

final String textMain = "text text";

huangapple
  • 本文由 发表于 2020年10月19日 01:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64416553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定