嗨我在這裏有這些類: 正如您所看到的,Faculty和Student從Persons繼承名稱。 而教師和學生都有其獨特的屬性與和如何在使用JAXB從XML文件中讀取時使用繼承
public class Persons{
protected String name;
public Persons(){
this(null);
}
public Persons(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public String setName(String name){
this.name = name;
}
}
public class Student extends Persons{
private String studentId;
public Student(){
this(null,null);
}
public Student(String name,String studentId){
this.name = name;
this.studentId = studentId;
}
}
public class Faculty extends Persons{
private String facultyId;
public Faculty(){
this(null,null);
}
public Faculty(String name,String facultyId){
this.name = name;
this.facultyId = facultyId;
}
}
只要我有這些XML文件:
<persons>
<student>
<name>Example 1</name>
<studentid>id</studentid>
</student>
<student>
<name>Example 1</name>
<studentid>id</studentid>
</student>
</persons>
和教師XML文件:
<persons>
<faculty>
<name>Example 1</name>
<facultyid>id</facultyid>
</faculty>
<faculty>
<name>Example 1</name>
<facultyid>id</facultyid>
</faculty>
</persons>
如何我是否會設置從xml文件解組的類。我用普通的xml文件做了這些,但沒有繼承,但我只是想知道如何在不寫兩個類中的相同屬性的情況下做到這一點。謝謝!
您可能會嘗試搜索一下。 http://stackoverflow.com/questions/16429717/jaxb-and-inheritance – 2014-10-26 20:13:31
我一直在尋找。我只是很難理解我想如何保存一份學生名單。我會在Person類還是Students類中設置該屬性?我知道你通常會這樣做:List學生 –
Nazariy1995
2014-10-26 20:51:43
@ Brenden1995沒有時間做完整的答案 - 但是你可以在模式方面用替代組和在JAXB方用'@XmlElementRef '。同時檢查'xsi:type'。 – lexicore 2014-10-27 13:34:20