Java ClassCastException类。不能将住宿分配给类。吸引

Java ClassCastException类。不能将住宿分配给类。吸引,java,polymorphism,classcastexception,Java,Polymorphism,Classcastexception,这些是我的蒙代尔: public class Accomodation { ... } 及 我在我的控制器类中有此方法: protected Accomodation getAccomodationByFormData(CrudView crudView) { Accomodation accomodation = new Accomodation(); // setters return accomodation; } 在At

这些是我的蒙代尔:

public class Accomodation {
    ...
}

我在我的
控制器
类中有此方法:

protected Accomodation getAccomodationByFormData(CrudView crudView) {
        Accomodation accomodation = new Accomodation();
        // setters
        return accomodation;
    }
AttractionController
中使用(它扩展了以前的
控制器
):

在这篇
AttractionController
的另一点中,我有:

...
Attraction attraction = (Attraction) getAccomodationByFormData(crudAttractionView); // throws ClassCastException
...
但这给了我:

线程“JavaFX应用程序线程”java.lang.ClassCastException中的异常:类模型。调节不能强制转换为类模型。吸引(models.accountation和models.Attraction位于加载器“app”的未命名模块中) 位于controllers.crudatractioncontroller.getAccountationByFormData(crudatractionController.java:302) 在controllers.crudatractioncontroller.lambda$按钮上单击$4(crudatractioncontroller.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.firevent(EventUtil.java:49) 位于javafx.base/javafx.event.event.fireEvent(event.java:198) 位于javafx.graphics/javafx.scene.Node.firevent(Node.java:8879) 位于javafx.controls/javafx.scene.control.Button.fire(Button.java:200) 在javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.MouseRelease(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.firevent(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.ProcessMouseeEvent(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.\u runLoop(本机方法) 在javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277) 位于java.base/java.lang.Thread.run(Thread.java:834)

(请注意,
crudatractionview
类扩展了
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
}
住宿

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
}
公共类住宿实现可序列化{
受保护的字符串id;
受保护的字符串名称;
保护整数;
受保护的整数avaragePrice;
受保护的字符串电话号码;
受保护地址;
保护点;
受保护名单审查;
受保护的长期审查;
受保护的长期总额定值;
受保护的列表图像;
受保护的布尔hasfecertificateofeexcellence;
受保护的字符串加法;
受保护字符串lastModificationDate;
公共住宿(){
}
Attraction attraction = (Attraction) super.getAccomodationByFormData(crudAttractionView);
Accomodation accomodation = super.getAccomodationByFormData(crudAttractionView);
Attraction attraction = new Attraction(accomodation);
attraction.setOpeningTime(getOpeningTimeWithFormData())
public Attraction(Accomodation accomodation){
  attraction.setX(accomodation.getX())
  ...
}
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
}