如何转发和使用特定于子的方法

我有以下问题:
//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模式生成的。在我的例子中,我可以重新设计模式,以便两种类型都有共同的基础。 但是如果我不能控制架构呢?我怎么能实现基本的想法?     
已邀请:
如果您可以控制模式,则可以定义其他类型可以扩展的基本抽象类型。我自己没有尝试过,所以我不确定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");
    }
}
任何问题都可以用另一层间接解决.....除了太多的间接。     
我建议你让这两个类实现一个公共接口,然后转换为它。我看不出你当前的演员会有什么影响......
public interface NamedElement
{
    String getName();
}

...

NamedElement department = a == 1 ? order.getDepartment() : 
                                   order.getOrganisation();
String name = department.getName();
假设您可以控制
DepartmentType
OrganizationType
代码。     
注意:这是您拥有的当前类层次结构的替代方法。 定义一个名为UnitTypeBase的intermdeiate类,它从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 ... 
    
使用方法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();
    

要回复问题请先登录注册