简体   繁体   English

如何从java中的父对象获取子对象?

[英]How to get the child object from the parent in java?

I am getting trouble in an application developement. 我在应用程序开发中遇到麻烦。 My task is i have a service object containing set of person objects. 我的任务是我有一个包含一组人物对象的服务对象。 the person object has two derived objects called student and teacher. person对象有两个派生对象,称为student和teacher。 This service object i am getting from client to server as a json object. 我作为json对象从客户端到服务器获取此服务对象。 At server side i am able to get the List<Person> object from the service by using service.getPersonList() method. 在服务器端,我可以使用service.getPersonList()方法从服务中获取List<Person>对象。 But i am not able to get the child classes of Person . 但是我无法获得Person的子类。 My code is like this 我的代码是这样的

//person //人

public class Person {

    private String name;
    private Integer type;

    public static final Integer TYPE_STUDENT = 1 ;
    public static final Integer TYPE_TEACHER = 2 ;

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

//student //学生

public class Student extends Person{

    private Integer year;

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }
}

//teacher //老师

public class Teacher extends Person{

    private String dept;

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }
}

//service //服务

public class Service {

    private List<Person> personList;

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }
}

// my json object is like this //我的json对象是这样的

[{"year":"4","name":"sreenu","type":1},{"dept":"physics","name":"somehow","type":2}]

I am trying to find the child class by using type property. 我试图通过使用type属性来查找子类。 I am getting type value means parent object but not able to fetch child from that. 我得到类型值意味着父对象,但无法从中获取子对象。 How can i solve this problem? 我怎么解决这个问题?

In controller i am getting the json object like this 在控制器中我得到像这样的json对象

//controller public String saveService(@RequestBody String service) { // controller public String saveService(@RequestBody String service){

List<Person> personList = (List<Person>) gson.fromJson(
    data, new TypeToken<List<Person>>(){}.getType()
);
for(Person person : personList) {
    if(person.getType() ==  Person.TYPE_STUDENT) {
        Student student = (Student) person;
        System.out.println(student.getYear());
    }
    if(person.getType() ==  Person.TYPE_TEACHER) {
        Teacher teacher = (Teacher) teacher;
        System.out.println(teacher.getDept());
    }
}

} }

I am getting class cast exception. 我正在获得类强制转换异常。 Even i tried with instance of operator. 即使我尝试过运营商的例子。

You are deserializing to a simpler type (Person) rather than a more complex type (Student/Teacher). 您正在反序列化为更简单的类型(Person)而不是更复杂的类型(Student / Teacher)。 This means that additional subtype information (like Year or Department) is being discarded. 这意味着正在丢弃其他子类型信息(如年份或部门)。 A Student can be cast as a Person, but a Person cannot be cast as a Student. 学生可以作为一个人进行演员,但不能将一个人演员作为学生。

This reflects a design problem. 这反映了设计问题。 Either you will need to change your Person class so that it contains all the possible data that a Subtype needs, or you will need to preemptively segregate your data into Teachers and Students, before creating your objects from Json. 您需要更改Person类,使其包含子类型所需的所有可能数据,或者在从Json创建对象之前,您需要抢先将数据隔离到教师和学生中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM