使用不同的变量创建相同 class 的对象
Create objects of the same class with different variables
我想知道是否可以在 Java 中每次创建具有不同变量的相同 class 的对象。
示例:
此方法 returns 来自 URL 的数据和 JSON 文件,我想用它来创建一个 post 对象。
ObjectMapper mapper = new ObjectMapper();
public Post getData(String ... url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
return mapper.readValue(new URL(url[0]), Post.class);
}
但是 .JSON 可能是:
{ "name": "Bob" }
还有:
{ "color": "Red", "width": "200", "height": "150" }
或其他。
如果 Post
是 Map 或 JsonNode 的子类,那么是的,它可以包含任何 key-value 对。
如果Post
看起来像这样,那么不,它被严格定义为只有一个字段。
public class Post {
String name;
}
我想知道是否可以在 Java 中每次创建具有不同变量的相同 class 的对象。
示例:
此方法 returns 来自 URL 的数据和 JSON 文件,我想用它来创建一个 post 对象。
ObjectMapper mapper = new ObjectMapper();
public Post getData(String ... url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
return mapper.readValue(new URL(url[0]), Post.class);
}
但是 .JSON 可能是:
{ "name": "Bob" }
还有:
{ "color": "Red", "width": "200", "height": "150" }
或其他。
如果 Post
是 Map 或 JsonNode 的子类,那么是的,它可以包含任何 key-value 对。
如果Post
看起来像这样,那么不,它被严格定义为只有一个字段。
public class Post {
String name;
}