ClassCastException class.Accomodation 无法转换为 class.Attraction
ClassCastException class.Accomodation cannot be cast to class.Attraction
这些是我的模特:
public class Accomodation {
...
}
和
public class Attraction extends Accomodation {
...
private String openingTime;
}
我的 Controller
class:
中有这个方法
protected Accomodation getAccomodationByFormData(CrudView crudView) {
Accomodation accomodation = new Accomodation();
// setters
return accomodation;
}
在 AttractionController
中使用(扩展了之前的 Controller
):
@Override
protected Accomodation getAccomodationByFormData(CrudView crudView) {
Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);
attraction.setOpeningTime(getOpeningTimeWithFormData());
return attraction;
}
另外一点 AttractionController
我有:
...
Attraction attraction = (Attraction) getAccomodationByFormData(crudAttractionView); // throws ClassCastException
...
但这给了我:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class models.Accomodation cannot be cast to class models.Attraction (models.Accomodation and models.Attraction are in unnamed module of loader 'app')
at controllers.CrudAttractionController.getAccomodationByFormData(CrudAttractionController.java:302)
at controllers.CrudAttractionController.lambda$buttonConfermaClicked(CrudAttractionController.java:143)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277)
at java.base/java.lang.Thread.run(Thread.java:834)
(Ps。注意 CrudAttractionView
class 扩展了 CrudView
)
- 我该如何解决?
编辑
景点
public class Attraction extends Accomodation {
private String openingTime;
public Attraction() {
}
public Attraction(Accomodation accomodation) {
super(accomodation);
}
// getters and setters
}
住宿
public class Accomodation implements Serializable {
protected String id;
protected String name;
protected Integer avarageRating;
protected Integer avaragePrice;
protected String phoneNumber;
protected Address address;
protected Point point;
protected List<Review> reviews;
protected Long totalReviews;
protected Long totalRating;
protected List<String> images;
protected boolean hasCertificateOfExcellence;
protected String addedDate;
protected String lastModificationDate;
public Accomodation() {
}
public Accomodation(Accomodation accomodation) {
}
// getters and setters
}
所有 Attraction
都是 Accomodation
,但并非所有 Accomodation
都是 Attraction
。
这意味着你不能安全地做:
Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);
即使您在 AttractionController
中(java 不会根据您的控制器名称更改 super.getAccomadation 的 return 类型)。
一个不需要任何框架的解决方案是做类似的事情:
Accomodation accomodation = super.getAccomodationByFormData(crudAttractionView);
Attraction attraction = new Attraction(accomodation);
attraction.setOpeningTime(getOpeningTimeWithFormData())
您只需要确保您有一个 Attraction
构造函数,它将接受 Accomodation
并将其字段复制到您的景点。
类似于:
public Attraction(Accomodation accomodation){
attraction.setX(accomodation.getX())
...
}
或者只是调用 super(accomodation)
并在 Accomodation
中拥有一个构造函数,它将能够将一个住宿复制到一个新的住宿中(这样您就可以在以后的所有扩展中使用该构造函数类)
现在,您确保您的住宿实际上是一个景点,然后您将其投放。
在一个更简洁的示例中,它看起来像这样
import java.io.Serializable;
class Attraction extends Accomodation {
public Attraction() {
}
public Attraction(Accomodation accomodation) {
super(accomodation); // we let super do the simple mapping
}
// getters and setters
public void setOpeningTime(String test){
//
}
protected Accomodation getAccomodationByFormData() {
Accomodation accomodation= new Accomodation();//super.getAccomodationByFormData(crudAttractionView);
Attraction attraction = new Attraction(accomodation); // we convert our accomodation thanks to our constructor
attraction.setOpeningTime("...");
return attraction;
}
}
class Accomodation implements Serializable {
protected String id;
protected String name;
protected Integer avarageRating;
protected Integer avaragePrice;
protected String phoneNumber;
protected Long totalReviews;
protected Long totalRating;
protected boolean hasCertificateOfExcellence;
protected String addedDate;
protected String lastModificationDate;
public Accomodation(Accomodation accomodation) {
this.id = accomodation.id;
this.name = accomodation.name;
this.avarageRating = accomodation.avarageRating;
this.avaragePrice = accomodation.avaragePrice;
this.phoneNumber = accomodation.phoneNumber;
this.totalReviews = accomodation.totalReviews;
this.totalRating = accomodation.totalRating;
this.hasCertificateOfExcellence = accomodation.hasCertificateOfExcellence;
this.addedDate = accomodation.addedDate;
this.lastModificationDate = accomodation.lastModificationDate;
}
public Accomodation() {
}
// getters and setters
}
这些是我的模特:
public class Accomodation {
...
}
和
public class Attraction extends Accomodation {
...
private String openingTime;
}
我的 Controller
class:
protected Accomodation getAccomodationByFormData(CrudView crudView) {
Accomodation accomodation = new Accomodation();
// setters
return accomodation;
}
在 AttractionController
中使用(扩展了之前的 Controller
):
@Override
protected Accomodation getAccomodationByFormData(CrudView crudView) {
Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);
attraction.setOpeningTime(getOpeningTimeWithFormData());
return attraction;
}
另外一点 AttractionController
我有:
...
Attraction attraction = (Attraction) getAccomodationByFormData(crudAttractionView); // throws ClassCastException
...
但这给了我:
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class models.Accomodation cannot be cast to class models.Attraction (models.Accomodation and models.Attraction are in unnamed module of loader 'app') at controllers.CrudAttractionController.getAccomodationByFormData(CrudAttractionController.java:302) at controllers.CrudAttractionController.lambda$buttonConfermaClicked(CrudAttractionController.java:143) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.base/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879) at javafx.controls/javafx.scene.control.Button.fire(Button.java:200) at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206) at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.base/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851) at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433) at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556) at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942) at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop(GtkApplication.java:277) at java.base/java.lang.Thread.run(Thread.java:834)
(Ps。注意 CrudAttractionView
class 扩展了 CrudView
)
- 我该如何解决?
编辑
景点
public class Attraction extends Accomodation {
private String openingTime;
public Attraction() {
}
public Attraction(Accomodation accomodation) {
super(accomodation);
}
// getters and setters
}
住宿
public class Accomodation implements Serializable {
protected String id;
protected String name;
protected Integer avarageRating;
protected Integer avaragePrice;
protected String phoneNumber;
protected Address address;
protected Point point;
protected List<Review> reviews;
protected Long totalReviews;
protected Long totalRating;
protected List<String> images;
protected boolean hasCertificateOfExcellence;
protected String addedDate;
protected String lastModificationDate;
public Accomodation() {
}
public Accomodation(Accomodation accomodation) {
}
// getters and setters
}
所有 Attraction
都是 Accomodation
,但并非所有 Accomodation
都是 Attraction
。
这意味着你不能安全地做:
Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);
即使您在 AttractionController
中(java 不会根据您的控制器名称更改 super.getAccomadation 的 return 类型)。
一个不需要任何框架的解决方案是做类似的事情:
Accomodation accomodation = super.getAccomodationByFormData(crudAttractionView);
Attraction attraction = new Attraction(accomodation);
attraction.setOpeningTime(getOpeningTimeWithFormData())
您只需要确保您有一个 Attraction
构造函数,它将接受 Accomodation
并将其字段复制到您的景点。
类似于:
public Attraction(Accomodation accomodation){
attraction.setX(accomodation.getX())
...
}
或者只是调用 super(accomodation)
并在 Accomodation
中拥有一个构造函数,它将能够将一个住宿复制到一个新的住宿中(这样您就可以在以后的所有扩展中使用该构造函数类)
现在,您确保您的住宿实际上是一个景点,然后您将其投放。
在一个更简洁的示例中,它看起来像这样
import java.io.Serializable;
class Attraction extends Accomodation {
public Attraction() {
}
public Attraction(Accomodation accomodation) {
super(accomodation); // we let super do the simple mapping
}
// getters and setters
public void setOpeningTime(String test){
//
}
protected Accomodation getAccomodationByFormData() {
Accomodation accomodation= new Accomodation();//super.getAccomodationByFormData(crudAttractionView);
Attraction attraction = new Attraction(accomodation); // we convert our accomodation thanks to our constructor
attraction.setOpeningTime("...");
return attraction;
}
}
class Accomodation implements Serializable {
protected String id;
protected String name;
protected Integer avarageRating;
protected Integer avaragePrice;
protected String phoneNumber;
protected Long totalReviews;
protected Long totalRating;
protected boolean hasCertificateOfExcellence;
protected String addedDate;
protected String lastModificationDate;
public Accomodation(Accomodation accomodation) {
this.id = accomodation.id;
this.name = accomodation.name;
this.avarageRating = accomodation.avarageRating;
this.avaragePrice = accomodation.avaragePrice;
this.phoneNumber = accomodation.phoneNumber;
this.totalReviews = accomodation.totalReviews;
this.totalRating = accomodation.totalRating;
this.hasCertificateOfExcellence = accomodation.hasCertificateOfExcellence;
this.addedDate = accomodation.addedDate;
this.lastModificationDate = accomodation.lastModificationDate;
}
public Accomodation() {
}
// getters and setters
}