使用SuperClass方法的Instantiation Object

时间:2016-04-15 13:34:08

标签: java spring

我正在使用Spring Framework开发JAVA EE项目。我读了一篇关于wa如何使用带有@MappedSuperclass注释的SupperClass的教程。

课程如下:

@MappedSuperclass
public class AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected Long id;

@Version
protected Long version;

@Override
 public int hashCode() {
 int hash = 0;
 hash += (id != null ? id.hashCode() : 0);
 return hash;
 }

//initialisation
public AbstractEntity build(Long id,Long version){
    this.id=id;
    this.version=version;
    return this;
}

@Override
public boolean equals(Object entity){
    String class1= this.getClass().getName();

    String class2=entity.getClass().getName();
    if(!class2.equals(class1)){
        return false;
    }

    AbstractEntity other= (AbstractEntity) entity;
    return this.id == other.id;
}

}

@MappedSuperclass
public class Personne extends AbstractEntity {

private static final long serialVersionUID = 1L;

@Column(length = 5)
private String titre;
@Column(length=20)
private String nom;
@Column(length=20)
private String prenom;

public Personne(String titre, String nom, String prenom) {
    super();
    this.titre = titre;
    this.nom = nom;
    this.prenom = prenom;
}

public Personne() {
}

//ToString
public String toString(){
    return String.format("Personne[%s, %s, %s, %s]",id,version,titre,nom,prenom);
}

public String getTitre() {
    return titre;
}

public void setTitre(String titre) {
    this.titre = titre;
}

public String getNom() {
    return nom;
}

public void setNom(String nom) {
    this.nom = nom;
}

public String getPrenom() {
    return prenom;
}

public void setPrenom(String prenom) {
    this.prenom = prenom;
}

}

@Entity
@Table(name = "medecins")
public class Medecin extends Personne {

private static final long serialVersionUID = 1L;

public Medecin(){

}

public Medecin(String titre, String nom, String prenom) {
    super(titre, nom, prenom);
}

}

我没有理解的是Medecin对象的这种情况:

     Medecin m=new Medecin("Mr","Paul","Tatou").build(10,1);

由于AbstratcEntity是一个具有构建方法的超类,我们如何实现这样的子对象,是否与多态原理相矛盾?

0 个答案:

没有答案