首页 \ 问答 \ Java有界的类型(Java bounded types)

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
更新时间:2023-05-11 07:05

最满意答案

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.

相关问答

更多