我有一个类
类别:StatusEventChangeDto ::
public class StatusEventChangeDto {
protected String address;
DutyState status;
protected double latitude,longitude;
protected long time;
public StatusEventChangeDto()
{}
public StatusEventChangeDto(double latitude, double longitude, String address, long time, DutyState status)
{
this.latitude = latitude;
this.longitude = longitude;
this.address = address;
this.time = time;
this.status = status;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public DutyState getStatus() {
return status;
}
public void setStatus(DutyState status) {
this.status = status;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Override
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("Lat : "+latitude+"\n");
sb.append("Long : "+longitude+"\n");
sb.append("Address : "+address+"\n");
sb.append("Time : "+time+"\n");
sb.append("Status : "+status+"\n");
return sb.toString();
}
}
还有一个课程
类别:StatusEventDurationChangeDto
public class StatusEventDurationChangeDto extends StatusEventChangeDto
{
private Duration duration;
public StatusEventDurationChangeDto()
{
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public String toString() {
StringBuffer sb = new StringBuffer(super.toString());
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendHours()
.appendSuffix("h")
.appendMinutes()
.appendSuffix("m")
.toFormatter();
String formatted = formatter.print(duration.toPeriod());
sb.append("Duration : "+formatted+"\n");
return sb.toString();
}
}
我在这里输入类似于上面的两个类似
StatusEventChangeDto event1 = new StatusEventChangeDto(17.45, 78.45, "hyderabad, Telangana", now.minusHours(1).getMillis(), ONDUTY);
StatusEventChangeDto event2 = new StatusEventChangeDto(17.48, 78.50, "hyderabad, Telangana", now.minusHours(2).getMillis(), OFFDUTY);
StatusEventDurationChangeDto duration1 = (StatusEventDurationChangeDto)event1;
duration1.setDuration(new Duration(now, now.minusHours(1)));
StatusEventDurationChangeDto duration2 = (StatusEventDurationChangeDto) event2;
duration2.setDuration(new Duration(now, now.minusHours(2)));
当我运行这个时,我得到了类型转换异常
帮助我的家伙
答案 0 :(得分:0)
您无法将event1
和event1
投射到StatusEventDurationChangeDto
,因为此变量已链接到对象StatusEventChangeDto
的实例,但不会延伸StatusEventDurationChangeDto
反之亦然
答案 1 :(得分:0)
问题在于:
StatusEventDurationChangeDto duration1 =(StatusEventDurationChangeDto)event1;
您无法将event1 / 2的对象强制转换为StatusEventDurationChangeDto,因为event1 / event2属于StatusEventChangeDto类型(您自己定义的类)
我认为您应该将该课程视为" USER DEFINED DATA TYPE"。您应该首先采用类和类型转换的完整概念。
答案 2 :(得分:0)
你有
StatusEventDurationChangeDto将StatusEventChangeDto扩展为
表示StatusEventDurationChangeDto是一个StatusEventChangeDto,因此您可以将StatusEventDurationChangeDto强制转换为StatusEventChangeDto,但不能将StatusEventChangeDto强制转换为StatusEventDurationChangeDto。
一般规则是,您可以在层次结构中向下投射某些东西而不是反过来。