1. 程式人生 > >java實體Bean 父子類轉換

java實體Bean 父子類轉換

開發中遇到一個問題,父類轉子類問題

public class StudentCityArea  extends Student{

    private static final long serialVersionUID = -4004994377623377635L;
    private String province;
    private String city;
    private String area;
    private String remark;
}

直接使用強轉出問題

      Student student=new Student();
      StudentCityArea StudentCityAread123=(StudentCityArea)student;

     Exception in thread "main" java.lang.ClassCastException

解決辦法,利用json 轉化生成子類,解決問題

 Student student=new Student();
 String studentStr= JSONObject.toJSONString(student);
StudentCityArea studentCityArea
=JSONObject.parseObject(studentStr,StudentCityArea.class);