Java有界的类型(Java bounded types)
我试图了解Java中的有界类型。 我认为我的推理几乎是正确的,但我得到一个错误,我不明白为什么它给了我这个错误,如果A(JsonPerson)是T(Person)的子类。 错误是以下(也在代码中注释):
Error:(22, 16) error: incompatible types: JsonPerson cannot be converted to A where A is a type-variable: A extends Person declared in method <A>fromJson(JSONObject)
返回行中出现错误“发生”。
我做了一个简单的例子,这里是代码
Person.java
public class Person { private String name; private String surname1; private String surname2; private String phone; private String email; public Person(String name, String surname1, String surname2, String phone, String email) { this.name = name; this.surname1 = surname1; this.surname2 = surname2; this.phone = phone; this.email = email; } public String getName() { return name; } public String getSurname1() { return surname1; } public String getSurname2() { return surname2; } public String getPhone() { return phone; } public String getEmail() { return email; } }
JsonPerson.Java
public class JsonPerson extends Person implements JSONSerializableInterface<Person> { public JsonPerson(String name, String surname1, String surname2, String phone, String email) { super(name, surname1, surname2, phone, email); } /** * Error:(22, 16) error: incompatible types: JsonPerson cannot be converted to A * where A is a type-variable: * A extends Person declared in method <A>fromJson(JSONObject) * */ @Override public <A extends Person> A fromJson(JSONObject json) throws JSONException { String name = json.getString("name"); String surname1 = json.getString("surname1"); String surname2 = json.getString("surname2"); String phone = json.getString("phone"); String email = json.getString("email"); return new JsonPerson(name, surname1, surname2, phone, email); } @Override public JSONObject toJson(Person object) { return null; } }
JSONSerializableInterdace.java
public interface JSONSerializableInterface<T> { public <A extends T> A fromJson(JSONObject json) throws JSONException; public JSONObject toJson(T object); }
I'm trying to understand bounded types in Java. I think that my reasoning is almost correct but I get an error and I don't understand why it gives me that error if A (JsonPerson) is a subclass of T (Person). The error is the following (also commented in the code):
Error:(22, 16) error: incompatible types: JsonPerson cannot be converted to A where A is a type-variable: A extends Person declared in method <A>fromJson(JSONObject)
The error "happens" in the return line.
I've made a simple example, here is the code
Person.java
public class Person { private String name; private String surname1; private String surname2; private String phone; private String email; public Person(String name, String surname1, String surname2, String phone, String email) { this.name = name; this.surname1 = surname1; this.surname2 = surname2; this.phone = phone; this.email = email; } public String getName() { return name; } public String getSurname1() { return surname1; } public String getSurname2() { return surname2; } public String getPhone() { return phone; } public String getEmail() { return email; } }
JsonPerson.Java
public class JsonPerson extends Person implements JSONSerializableInterface<Person> { public JsonPerson(String name, String surname1, String surname2, String phone, String email) { super(name, surname1, surname2, phone, email); } /** * Error:(22, 16) error: incompatible types: JsonPerson cannot be converted to A * where A is a type-variable: * A extends Person declared in method <A>fromJson(JSONObject) * */ @Override public <A extends Person> A fromJson(JSONObject json) throws JSONException { String name = json.getString("name"); String surname1 = json.getString("surname1"); String surname2 = json.getString("surname2"); String phone = json.getString("phone"); String email = json.getString("email"); return new JsonPerson(name, surname1, surname2, phone, email); } @Override public JSONObject toJson(Person object) { return null; } }
JSONSerializableInterdace.java
public interface JSONSerializableInterface<T> { public <A extends T> A fromJson(JSONObject json) throws JSONException; public JSONObject toJson(T object); }
原文:https://stackoverflow.com/questions/31743190
最满意答案
preg_match
返回一个数组。 机会是$quote[0]
将返回价格。尝试做
print_r($quote);
并查看您所需的结果是否在数组中,以及它所在的位置。
preg_match
returns an array. chances are$quote[0]
will return the price.try doing
print_r($quote);
and seeing if your desired result is in the array, and where it's located.
相关问答
更多-
大败而逃(打一电脑用语)[2023-02-26]
输出 -
无法从http get url成功函数输出数组到html上(Cannot output array from http get url success function onto the html)[2023-07-21]
首先定义$scope.returnlist = {}; 这是一个不是数组的对象。 然后,当您添加项目时,您指定的数组不是数组项目: $scope.returnlist[0] = [{ ID: newID, Name: newname }]; 所以你的最终回复列表如下: {0: [{ ID: newID, Name: newname }], 1:[{ ID: newID, Name: newname }],...} 一个具有数组值的对象,所以当你执行$ scope.returnlist [0] .ID时, ... -
JSON输出未显示(JSON output not being displayed)[2021-11-13]
jQuery.each()的第一个参数是值的索引,第二个参数是值。 解决方案更改$.each(json, function(v) { to $.each(json, function(iv) { function getAllDetails() { var myTable = ''; myTable += '