JAVA继承/多态与2个类

时间:2017-10-03 09:38:23

标签: java oop inheritance polymorphism eclipselink

我的系统中有事件,可能是事故观察。在控制器中,我有事故观察的类似代码工作流程。我正面临着了解我的问题是java继承还是多态的问题。

Java继承?

  • 父母 - 事件
  • 儿童 - 意外观察

多态性吗 事件可以 意外观察

我尝试创建一个名为Incident.java的接口,以涵盖事故和观察,并可能删除代码重复。 (这显示在 Incident.java 下面的末尾)但是我无法理解如何实现这种继承。

如何定义Incident.java?作为父母或事故/观察的界面?。 请帮我删除controller.java中的代码重复项。

代码: Controller.java

    public class Controller extends ODataService {

        public void processIncidents(){    
            List<Accident> allAccidents = accidentDAO.getAll();
            for(Accident selectedAccident : allAccidents){
                createAlertsForAccident( selectedAccident); 
            }

            //possible change to incident? so we can remove this code?
            List<Observation> allObservations = observationDAO.getAll();
            for(Observation selectedObservation : allObservations){
                createAlertsForObservation( selectedObservation);
            }
        }

        public Observation createAlertsForObservation(Observation selectedObservation){         
            String notification ="INCIDENT #ID has been reported successfully by NAME.";

            Alert alert = new Alert(notification);
            selectedObservation.addAlert(alert);
            observationDAO.save(selectedObservation);
        }

//similar code as above for accident - duplicate
        public Observation createAlertsForAccident(Accident selectedAccident){
            String notification ="INCIDENT #ID has been reported successfully by NAME.";
    Alert alert = new Alert(notification);
            selectedAccident.addAlert(alert);
            accidentDAO.save(selectedAccident);
        }
    }

Accident.java

@Entity
@Table(name = "ACCIDENTS")
public class Accident implements IDBEntity{

    @Id
    @GeneratedValue
    @Column(name = "ACCIDENT_ID")
    private Long id;

    @Basic
    private String type;

    @Basic
    private String place;

    @Basic
    private int numberInjured;

    @Basic
    private String description;

    @Column(name = "OCCR_DT_TIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar occuredDateTime;

    @Basic
    @Column(name = "PEND_USR_ID")
    private String pendingWith;

    @Basic
    @Column(name = "PEND_USR_ROLE")
    private String pendingWithUserRole;

    @Basic
    @Column
    private String pendingWithReason;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "accident", fetch = FetchType.EAGER, targetEntity = Injury.class)
    private List<Injury> injuries;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "accident", fetch = FetchType.EAGER, targetEntity = Alert.class)
    private List<Alert> alerts;

    <-- getters and setters -- >

Observation.java

 @Entity
    @Table(name = "OBSERVATIONS")
    public class Observation extends Incident implements IDBEntity {

        @Id
        @GeneratedValue
        @Column(name = "OBS_ID")
        private Long id;

        @Basic
        private String type;

        @Basic
        private String place;

        @Basic
        private String category;

        @Basic
        private String subCategory;

        @Basic
        private String description;

        @Basic
        private String occuredDtTimeStr;

        @Basic
        @Column(name = "PEND_USR_ID")
        private String pendingWith;

        @Basic
        @Column(name = "PEND_USR_ROLE")
        private String pendingWithUserRole;

        @Basic
        @Column
        private String pendingWithReason;

        @Column(name = "OCCR_DT_TIME")
        @Temporal(TemporalType.TIMESTAMP)
        private Calendar occuredDateTime;

        @Basic
        private boolean risk;

        @Basic
        private boolean hazard;

        @OneToMany(cascade = CascadeType.ALL, mappedBy = "observation", fetch = FetchType.EAGER, targetEntity = Alert.class)
        private List<Alert> alerts;
        <-- getters and setters -- >

Incident.java ????

public class Incident implements IDBEntity{
        ???
        @Override
        public Long getId() {
            // TODO Auto-generated method stub
            return null;
        }


        public Location getLocation() {
            // TODO Auto-generated method stub
            return null;
        }

        public void setPendingWith(String actorName) {
            // TODO Auto-generated method stub

        }

        public void setPendingWithUserRole(String actorRole) {
            // TODO Auto-generated method stub

        }

        public void setPendingWithReason(String properNotificationWithReplacements) {
            // TODO Auto-generated method stub

        }

    }

0 个答案:

没有答案