我有以下问题:
//class XmlObject is part of org.apache.xmlbeans
public class DepartmentType extends XmlObject; // getName method is defined in this class
public class OrganizatiopnType extends XmlObject; // getName method is defined in this class
XmlObject department = null;
if (a == 1)
department = (DepartmentType) order.getDepartment(); // returns DepartmentType
else
department = (OrganizationType) order.getOrganization(); // returns OrganizationType
department.getName(); // throws cannot find symbol
// ... and do some other complex stuff using methods which are defined in both classes ...
调用getName()方法的最简洁方法是什么?
更新1:
Cybernate,如果你能控制DepartmentType
& OrganizationType
。不幸的是,这些对象是由xmlbeans从XML模式生成的。就我而言,我可以重新设计架构,这样两种类型都有共同的基础。
但是如果我不能控制架构呢?我怎么能实现这个基本想法?
答案 0 :(得分:2)
我建议你让这两个类实现一个公共接口,然后转换为它。我看不出你当前的演员会有什么影响......
public interface NamedElement
{
String getName();
}
...
NamedElement department = a == 1 ? order.getDepartment() :
order.getOrganisation();
String name = department.getName();
这假设您可以控制DepartmentType
和OrganizationType
代码。
答案 1 :(得分:2)
注意:这是您拥有的当前类层次结构的替代方法。
定义一个名为 UnitTypeBase 的中间类,其中扩展来自 XmlObject 。 像
这样的东西public class UnitTypeBase extends XmlObject{
public String getName(){
//Some implementaition or you can mark it as abstract
}
}
然后从 UnitTypeBase
派生 * DepartmentType和OrganizationType *//class XmlObject is part of org.apache.xmlbeans
public class DepartmentType extends UnitTypeBase; // getName method is defined in this class
public class OrganizatiopnType extends UnitTypeBase; // getName method is defined in this class
UnitTypeBase department = null;
if (a == 1)
department = (DepartmentType) order.getDepartment(); // returns DepartmentType
else
department = (OrganizationType) order.getOrganization(); // returns OrganizationType
department.getName();
// ... and do some other complex stuff using methods which are defined in both classes ...
答案 2 :(得分:0)
使用方法getName创建接口,DepartmentType和OrganizationType这两个类都实现了此接口。如;
public class IName {
public void getName();
}
public class DepartmentType extends XmlObject implements IName {}
public class OrganizationType extends XmlObject implements IName {}
IName department = null;
if (a==1)
department = order.getDepartment();
else
department = order.getOrganization();
String name = department.getName();
答案 3 :(得分:0)
如果您可以控制架构,则可以定义其他类型可以扩展的基本抽象类型。我自己没有尝试过,所以我不确定XmlBeans将如何处理它。
<complexType name="NamedEntity" abstract="true">
<sequence>
<element name="name" type="string"/>
</sequence>
</complexType>
<complexType name="DepartmentType">
<complexContent>
<extension base="NamedEntity">
<sequence>
<element name="whatever"/>
</sequence>
</extension>
</complexContent>
</complexType>
否则,这是一个解决方法(黑客?)但你可以使用Commons BeanUtils,前提是你生成的类遵循JavaBean命名约定。如果这些对象被大量传递,你可以创建一个包装类来使调用更加具体。
public class Department {
XmlObject obj;
public Department(XmlObject obj){
if(!obj instanceof DepartmentType || !obj instanceof OrganizatiopnType){
throw new IllegalArgumentException();
}
this.obj = obj;
}
public String getName(){
return (String)PropertyUtils.getProperty(obj, "name");
}
}
任何问题都可以通过另一层间接来解决.....除了过多的间接。