2011-04-04 42 views
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模式生成的。在我的情况下,我可能重新设计架构,使这两种类型都有共同的基础。

但是如果我不能控制架构会怎么样。我怎么能实现这个基本想法?

回答

0

如果您控制了模式,则可以定义其他类型可以扩展的基本抽象类型。我没有自己尝试过,所以我不确定它是如何处理它的。

<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"); 
    } 
} 

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

2

我建议你让两个类都实现一个通用接口,然后转换为这个接口。我不能看到你目前的管型有什么影响?

public interface NamedElement 
{ 
    String getName(); 
} 

... 

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

这是假设你有过DepartmentTypeOrganizationType代码控制,当然。

+0

这就是问题所在,我无法控制'DepartmentType'&'OrganizationType',它们是由xml中的xmlbeans生成的。 – zeldi 2011-04-04 21:12:41

2

注意:这是您当前类层次结构的替代方法。

定义名为UnitTypeBase其中的XmlObject延伸一个intermdeiate类。 喜欢的东西

public class UnitTypeBase extends XmlObject{ 
public String getName(){ 
    //Some implementaition or you can mark it as abstract 
} 
} 

然后导出 * DepartmentType和OrganizationType *从UnitTypeBase

//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 ... 
+0

我会建议一个接口,而不是一个中间类 - 这样,如果在不同的类集合中必须有两个这样的接口,它将工作得很好。 – 2011-04-04 19:06:35

+0

@Jon:是的,这一切都取决于上下文..如果getName需要跨越不同类的基于接口的方法会更好。 – Chandu 2011-04-04 19:07:36

+0

如果我无法控制类型(它们是由externaly产生的),该怎么办? – zeldi 2011-04-04 21:18:58

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();