【发布时间】:2011-08-19 09:37:31
【问题描述】:
我有以下 XML 代码:
<stereotypes>
<stereotype1/>
<stereotype2/>
</stereotypes>
问题是我需要一个通用属性为每个原型类型具有不同的值。
实际上,我不确定这是否可行,或者我是否可以实现这样的事情。
我使用以下架构片段(设置路径属性)尝试了此操作。我想要的是为每个原型类型赋予这个属性一个固定值。目标是在 AbstractStereotype 类上生成 getPath 并以通用方式使用它。问题是我似乎找不到在特定 Stereotypes 中定义属性值的方法。
<xs:element name="stereotypes" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="stereotype1" type="Stereotype1" />
<xs:element name="stereotype2" type="Stereotype2"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="AbstractStereotype" abstract="true">
<xs:attribute name="path" type="amf-base:FQN" use="required"></xs:attribute>
</xs:complexType>
<xs:complexType name="Stereotype1">
<xs:complexContent>
<xs:extension base="AbstractStereotype">
<!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class1"/> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Stereotype2">
<xs:complexContent>
<xs:extension base="AbstractStereotype">
<!-- <xs:attribute name="path" type="amf-base:FQN" fixed="my.path.to.Class2"/> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
任何其他可以让我“在 AbstractStereotype 类上生成 getPath 方法并以通用方式使用它”的建议将不胜感激。
编辑:也许更清楚我需要的结果。
public abstract class AbstractStereotype {
public String getPath();
}
public class Stereotype1 extends AbstractStereotype {
public String getPath() {
return "Path1";
}
}
public class Stereotype2 extends AbstractStereotype {
public String getPath() {
return "Path2";
}
}
我需要这个,因为我想以同样的方式对待这些刻板印象:
public void someMethod() {
for(AbstractStereotype stereotype: getStereotypes()) {
System.out.println(stereotype.getPath());
}
}
正如我之前所说,甚至不确定使用这种方法是否可行。
【问题讨论】: