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 Application Thread”中的异常java.lang.classCastException:类模型。Accomodation无法转换为类模型。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) 在javafx.base / com.sun.javafx.event.BasicEventdispatcher.dispatchEvent(BasicEventdispatcher.java:58) 在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.EventdispatchChainImpl.dispatchEvent(EventdispatchChainImpl.java:114) 在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.Event.fireEvent(Event.java:198) 在javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879) 在javafx.controls / javafx.scene.control.Button.fire(Button.java:200) 在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:238) 在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在javafx.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) 在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.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: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) 在javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) 在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.run(GlassViewEventHandler.java:295) 在java.base / java.security.AccessController.doPrivileged(本机方法) 在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(本机方法) 在javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277) 在java.base / java.lang.Thread.run(Thread.java:834)

(请注意,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
}

解决方法

所有Attraction都是Accomodation,但并非所有Accomodation都是Attraction

这意味着您不能安全地进行操作:

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
}