英文:
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<String> texts;
public BertJsonRequest(int x, String text) {
this.id = x;
this.texts = new ArrayList<>();
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<String> texts = new ArrayList<>();
public BertJsonRequest(int x, String text) {
id = x;
texts.add(text);
}
}
答案2
得分: 0
我猜你的 BertJsonRequest
是 BertClient
的内部类。你无法在 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论