java 里面继承关系里 父类对象可以强制转换成子类吗? 并把父类对象的属性赋予给子类。。。??

qq_32040927 2018-01-15 11:21:51
//父类
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



...全文
984 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ThisSin 2018-01-17
  • 打赏
  • 举报
回复
引用 2 楼 u011594442 的回复:
不行,你这不是强行让爹装儿子么~~
没毛病,二楼这个解释精辟~~
自由自在_Yu 2018-01-17
  • 打赏
  • 举报
回复
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";
}
戎码亿升 2018-01-17
  • 打赏
  • 举报
回复
引用 2 楼 u011594442 的回复:
不行,你这不是强行让爹装儿子么~~
2楼的精辟,通俗易懂
shunji2099 2018-01-15
  • 打赏
  • 举报
回复
这个是不行的,编译不报错 但运行会报类型转换异常, 当获取的对象是父类,但是它实际上是某个子类的时候,你才可以用强转
Abin0406 2018-01-15
  • 打赏
  • 举报
回复
这个是不行的,接收类的范围必须比new 出来的类范围大,也就是说,父类= new 子类,这个是没有问题的,不可以反着执行
最猛的阿良 2018-01-15
  • 打赏
  • 举报
回复 1
引用 3 楼 MAX_WWei 的回复:
一切可强转 就是不符合逻辑
前提是你的父类对象就是子类new出来的
最猛的阿良 2018-01-15
  • 打赏
  • 举报
回复
一切可强转 就是不符合逻辑
Sunyiban 2018-01-15
  • 打赏
  • 举报
回复
不行,你这不是强行让爹装儿子么~~
飘逸的腿毛 2018-01-15
  • 打赏
  • 举报
回复
没问题,没报错

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧