//父类
public class OpenDoorInfo {
    private Long oid;    private String omanId;    private String omanName;    private String ophone;    private Date oouttime;    private String ocarid;    private String otype;    private Short isdelete;    private String oareaName;    private String ovillageidName;    private String ocameraName;    private String ohouseNum;    private String odeivName;    private Date createTime;    private long ocameraId;
    
public OpenDoorInfo() {
}
public OpenDoorInfo(Long oid, String omanId, String omanName, String ophone, Date oouttime, String ocarid,
String otype, Short isdelete, String oareaName, String ovillageidName, String ocameraName, String ohouseNum,
String odeivName, Date createTime, long ocameraId) {
super();
this.oid = oid;
this.omanId = omanId;
this.omanName = omanName;
this.ophone = ophone;
this.oouttime = oouttime;
this.ocarid = ocarid;
this.otype = otype;
this.isdelete = isdelete;
this.oareaName = oareaName;
this.ovillageidName = ovillageidName;
this.ocameraName = ocameraName;
this.ohouseNum = ohouseNum;
this.odeivName = odeivName;
this.createTime = createTime;
this.ocameraId = ocameraId;
}
/**
 * @return the oid
 */
public Long getOid() {
return oid;
}
/**
 * @param oid the oid to set
 */
public void setOid(Long oid) {
this.oid = oid;
}
/**
 * @return the omanId
 */
public String getOmanId() {
return omanId;
}
/**
 * @param omanId the omanId to set
 */
public void setOmanId(String omanId) {
this.omanId = omanId;
}
/**
 * @return the omanName
 */
public String getOmanName() {
return omanName;
}
/**
 * @param omanName the omanName to set
 */
public void setOmanName(String omanName) {
this.omanName = omanName;
}
/**
 * @return the ophone
 */
public String getOphone() {
return ophone;
}
/**
 * @param ophone the ophone to set
 */
public void setOphone(String ophone) {
this.ophone = ophone;
}
/**
 * @return the oouttime
 */
public Date getOouttime() {
return oouttime;
}
/**
 * @param oouttime the oouttime to set
 */
public void setOouttime(Date oouttime) {
this.oouttime = oouttime;
}
/**
 * @return the ocarid
 */
public String getOcarid() {
return ocarid;
}
/**
 * @param ocarid the ocarid to set
 */
public void setOcarid(String ocarid) {
this.ocarid = ocarid;
}
/**
 * @return the otype
 */
public String getOtype() {
return otype;
}
/**
 * @param otype the otype to set
 */
public void setOtype(String otype) {
this.otype = otype;
}
/**
 * @return the isdelete
 */
public Short getIsdelete() {
return isdelete;
}
/**
 * @param isdelete the isdelete to set
 */
public void setIsdelete(Short isdelete) {
this.isdelete = isdelete;
}
/**
 * @return the oareaName
 */
public String getOareaName() {
return oareaName;
}
/**
 * @param oareaName the oareaName to set
 */
public void setOareaName(String oareaName) {
this.oareaName = oareaName;
}
/**
 * @return the ovillageidName
 */
public String getOvillageidName() {
return ovillageidName;
}
/**
 * @param ovillageidName the ovillageidName to set
 */
public void setOvillageidName(String ovillageidName) {
this.ovillageidName = ovillageidName;
}
/**
 * @return the ocameraName
 */
public String getOcameraName() {
return ocameraName;
}
/**
 * @param ocameraName the ocameraName to set
 */
public void setOcameraName(String ocameraName) {
this.ocameraName = ocameraName;
}
/**
 * @return the ohouseNum
 */
public String getOhouseNum() {
return ohouseNum;
}
/**
 * @param ohouseNum the ohouseNum to set
 */
public void setOhouseNum(String ohouseNum) {
this.ohouseNum = ohouseNum;
}
/**
 * @return the odeivName
 */
public String getOdeivName() {
return odeivName;
}
/**
 * @param odeivName the odeivName to set
 */
public void setOdeivName(String odeivName) {
this.odeivName = odeivName;
}
/**
 * @return the createTime
 */
public Date getCreateTime() {
return createTime;
}
/**
 * @param createTime the createTime to set
 */
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/**
 * @return the ocameraId
 */
public long getOcameraId() {
return ocameraId;
}
/**
 * @param ocameraId the ocameraId to set
 */
public void setOcameraId(long ocameraId) {
this.ocameraId = ocameraId;
}
/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
return "OpenDoorInfo [oid=" + oid + ", omanId=" + omanId + ", omanName=" + omanName + ", ophone=" + ophone
+ ", oouttime=" + oouttime + ", ocarid=" + ocarid + ", otype=" + otype + ", isdelete=" + isdelete
+ ", oareaName=" + oareaName + ", ovillageidName=" + ovillageidName + ", ocameraName=" + ocameraName
+ ", ohouseNum=" + ohouseNum + ", odeivName=" + odeivName + ", createTime=" + createTime
+ ", ocameraId=" + ocameraId + "]";
}
}
//子类
public class OpenDoorInfoLinkImage extends OpenDoorInfo {

private List<CameraPicture> pictures;


public OpenDoorInfoLinkImage() {
}
public OpenDoorInfoLinkImage(Long oid, String omanId, String omanName, String ophone, Date oouttime,
String ocarid, String otype, Short isdelete, String oareaName, String ovillageidName,
String ocameraName, String ohouseNum, String odeivName, Date createTime, long ocameraId,
List<CameraPicture> pictures) {
super(oid, omanId, omanName, ophone, oouttime, ocarid, otype, isdelete, oareaName, ovillageidName,
ocameraName, ohouseNum, odeivName, createTime, ocameraId);
this.pictures = pictures;
}
public List<CameraPicture> getPictures() {
return pictures;
}
public void setPictures(List<CameraPicture> pictures) {
this.pictures = pictures;
}


}
//问题代码??
OpenDoorInfo odi=new OpenDoorInfoLinkImage();
OpenDoorInfoLinkImage ldli=(OpenDoorInfoLinkImage)odi;
报错信息:java.lang.ClassCastException: com.stkj.zjg.model.gzh.OpenDoorInfo cannot be cast to com.stkj.zjg.model.gzh.OpenDoorInfoLinkImage

解决方案 »

  1.   


    前提是你的父类对象就是子类new出来的
      

  2.   

    这个是不行的,接收类的范围必须比new 出来的类范围大,也就是说,父类= new 子类,这个是没有问题的,不可以反着执行
      

  3.   

    OpenDoorInfo odi=new OpenDoorInfoLinkImage();
    OpenDoorInfoLinkImage ldli=(OpenDoorInfoLinkImage)odi;
    这种写法应该是没有问题的呀,可以这样写
    public class Test {
    public static void main(String[] args) {
    FuClass fu = new ZiClass();//fu来源于ZiClass,这样不会有问题的
    ZiClass zi = (ZiClass) fu;
    System.out.println(zi.name);

    FuClass fu2 = new FuClass();
    ZiClass zi2 = (ZiClass) fu2;//这种会有问题java.lang.ClassCastException
    }
    }
    class FuClass{}
    class ZiClass extends FuClass{
    String name="11";
    }