1

我有以下问题:

//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:
Cyber​​nate,如果您可以控制DepartmentType& ,您的方法似乎是最合乎逻辑的OrganizationType。不幸的是,这些对象是由 xmlbeans 从 XML 模式生成的。就我而言,我可以重新设计模式,使两种类型都有共同的基础。

但是,如果我无法控制架构怎么办。我怎样才能实现基本思想?

4

4 回答 4

2

我建议你让这两个类都实现一个通用接口,然后转换为那个接口。我看不出你现在的演员阵容有什么影响......

public interface NamedElement
{
    String getName();
}

...

NamedElement department = a == 1 ? order.getDepartment() : 
                                   order.getOrganisation();
String name = department.getName();

当然,这是假设您可以控制DepartmentTypeOrganizationType代码。

于 2011-04-04T19:04:11.240 回答
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 ... 
于 2011-04-04T19:04:38.567 回答
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();
于 2011-04-04T19:12:41.117 回答
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>

否则,这是一种解决方法(hack?),但您可以使用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");
    }
}

任何问题都可以通过另一层间接来解决......除了太多的间接。

于 2011-04-04T22:06:58.553 回答