Java构造函数使用参数创建新列表

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

Java constructor use parameter to create new list

问题

新手学习Java我正在尝试创建一个类使用GSON将JSON字符串转换为POST请求并发送这个类是在名为 `BertClient` 的公共类中创建的

    private class BertJsonRequest {
        private Integer id;
        private List<String> texts;

        public BertJsonRequest(int x, String text) {
            this.id = x;
            this.texts = new ArrayList<>();
            this.texts.add(text);
        }
    }

我的使用方法

    BertJsonRequest rawRequestBody = new BertJsonRequest(1, text);
    Gson gsonToJson = new Gson();
    String requestBody = gsonToJson.toJson(rawRequestBody);

在我创建 `new BertJsonRequest` 的那一行我的IDE告诉我无法从静态内容中引用 BertClient.this

我想知道那是什么意思
我构造函数构建得对吗
我想不是我只想能够传入一个字符串以便构造函数可以使用该字符串创建一个字符串列表
英文:

New to Java. I'm trying to create a class to convert to JSON string to send as POST request using GSON. This class was created within a public class Called BertClient:

private class BertJsonRequest {
    private Integer id;
    private List&lt;String&gt; texts;

    public BertJsonRequest(int x, String text) {
        this.id = x;
        this.texts = new ArrayList&lt;&gt;();
        this.texts.add(text);
    }
}

How I use that:

 BertJsonRequest rawRequestBody = new BertJsonRequest(1, text);
 Gson gsonToJson = new Gson();
 String requestBody = gsonToJson.toJson(rawRequestBody);

For the line where I'm creating new BertJsonRequest My IDE tells me that BertClient.this cannot be referenced from a static content.

I wonder what that means.
Am I building the constructor correctly?
I think I'm not. I just want to be able to pass in a String so that constructor can create a List of String using that String.

答案1

得分: 1

你的类访问修饰符设置为private。尝试将访问修饰符改为public。

public class BertJsonRequest {
    private Integer id;
    private List<String> texts = new ArrayList<>();

    public BertJsonRequest(int x, String text) {
        id = x;
        texts.add(text);
    }
}
英文:

Your class access modifier is set to private. Try setting the access modifier to public instead.

public class BertJsonRequest {
    private Integer id;
    private List&lt;String&gt; texts = new ArrayList&lt;&gt;();

    public BertJsonRequest(int x, String text) {
        id = x;
        texts.add(text);
    }
}

答案2

得分: 0

我猜你的 BertJsonRequestBertClient 的内部类。你无法在 BertClient 外部实例化 BertJsonRequest。你可以将 BertJsonRequest 类声明为静态,以使其正常工作。

英文:

I guess your BertJsonRequest is a inner class of BertClient. You can't instantiate BertJsonRequest outside of BertClient. You can make BertJsonRequest class static for this to work.

答案3

得分: 0

根据我阅读你对他人答案的评论,我理解你的 BertClientRequest 可能是一个内部类。
如果确实是一个内部类,并且你试图在包含类的静态方法中调用它,那么显然你不能实例化你的内部类,因为该内部类不是 static

public class BertClient {
  private class BertClientRequest {
    /* 一些代码 */
  }
  
  static void aStaticMethod() {
    // ...
    // 内部类 BertClientRequest 对你的静态方法来说是未知的,因为它不是静态的,
    // 因此会导致编译时错误
    BertClientRequest rawRequest = new BertClientRequest(1, text);
    // ...
  }
}

在这种情况下,修复方法是将你的内部类更改为 static
private static class BertClientRequest

英文:

What I understood by reading your comments on other's answers was, that your BertClientRequest probably is an inner class.
In case it really is an inner class, and you try to call it in a static method of your containing class, it becomes apparent that you cannot instantiate your inner class as that inner class is not static.

public class BertClient {
  private class BertClientRequest {
    /* some code */
  }
  
  static void aStaticMethod() {
    // ...
    // Inner class BertClientRequest is unknown to your static method as it is not static,
    // thus giving you a compile time error
    BertClientRequest rawRequest = new BertClientRequest(1, text);
    // ...
  }
}

The fix would be in this case to change your inner class to static:
private static class BertClientRequest

huangapple
  • 本文由 发表于 2020年8月20日 12:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63498504.html
匿名

发表评论

匿名网友

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

确定