首页 > 解决方案 > ClassCastException class.Accomodation 不能转换为 class.Attraction

问题描述

这些是我的模型:

public class Accomodation {
    ...
}

public class Attraction extends Accomodation {
    ... 
    private String openingTime;
}

我在Controller课堂上有这个方法:

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
...

但这给了我:

线程“JavaFX 应用程序线程”java.lang.ClassCastException 中的异常:类models.Accomodation 无法转换为类models.Attraction(models.Accomodation 和models.Attraction 位于加载程序'app' 的未命名模块中)在controllers.CrudAttractionController.getAccomodationByFormData (CrudAttractionController.java:302) 在 controllers.CrudAttractionController.lambda$buttonConfermaClicked$4(CrudAttractionController.java:143) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在 javafx.base /com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 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) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base /com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在 javafx.base/javafx.event .javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879) at javafx.controls/javafx.scene.control.Button.fire(Button.java:200) 的 Event.fireEvent(Event.java:198)在 javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206) 在 javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap. java:274) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java: 80) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 javafx 的 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) .base/com.sun.javafx。event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 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) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在 javafx.base/javafx.event.Event.fireEvent(Event.java:198) 在 javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851) 在 javafx.graphics/javafx.scene .Scene$MouseHandler.access$1200(Scene.java:3579) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene. java:2588) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification。在 java.base/java.security.AccessController.doPrivileged(Native Method) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java: 434) 在 javafx.graphics/com.sun.javafx。tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433) 在 javafx.graphics/com.sun.glass。 ui.View.handleMouseEvent(View.java:556) 在 javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942) 在 javafx.graphics/com.sun.glass.ui.gtk。 GtkApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277) at java.base/java.lang.Thread.run(Thread.爪哇:834)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 $11(GtkApplication.java:277) 在 java.base/java.lang.Thread.run(Thread.java:834)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 $11(GtkApplication.java:277) 在 java.base/java.lang.Thread.run(Thread.java:834)

(Ps.注意CrudAttractionView类扩展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
}

标签: javapolymorphismclasscastexception

解决方案


都是,但不是Attraction全部。AccomodationAccomodationAttraction

这意味着你不能安全地做:

Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);

即使您在AttractionController(java 不会根据您的控制器名称更改 super.getAccomadation 的返回类型)。

不需要任何框架的一种解决方案是执行以下操作:

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它可以将一个 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
}

推荐阅读