【发布时间】:2018-08-22 15:25:10
【问题描述】:
我有这个 XML 结构:
<?xml version="1.0" encoding="UTF-8"?>
<specie name="Puma concolor" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="species.xsd">
<longevity>
<lifeSpan type="wild">
<min type="y">18</min>
<max type="y">20</max>
</lifeSpan>
<lifeSpan type="captivity">
<avg type="y">20</avg>
</lifeSpan>
</longevity>
<reproduction>
<matingSystem>polygamous/polygynous</matingSystem>
<gestation>
<min type="d">84</min>
<max type="d">106</max>
</gestation>
</reproduction>
</specie>
我创建了三个类 Min、Max 和 Avg,如下所示:
package speciejaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlType(name = "max")
public class Max {
private int _value;
public int getValue() {
return _value;
}
@XmlValue
public void setValue(int value) {
this._value = value;
}
private String _type;
public String getType() {
return _type;
}
@XmlAttribute
public void setType(String type) {
this._type = type;
}
}
如何在“longevity”类/元素和“reproduction/breedingInterval”类/元素中重用这些类?还是我必须复制它们并使用相同的代码创建 LongevityMin 和 BreedingMin?来自上层阶级的 setter/getter 呢?
【问题讨论】:
标签: java xml jaxb code-reuse