如何向上转换和使用子特定方法
我遇到以下问题:
//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
& ,您的方法似乎是最合乎逻辑的。 组织类型
。不幸的是,这些对象是由 xmlbeans 从 XML 模式生成的。就我而言,我可以重新设计架构,以便两种类型具有共同的基础。
但是如果我无法控制架构怎么办?我如何实现基本想法?
I have the following problem:
//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 ...
What is the cleanest way to call the getName() method?
UPDATE 1:
Cybernate, your approach seems the most logical, if you have control over the DepartmentType
& OrganizationType
. Unfortunately, these objects are generated from XML schema by xmlbeans. In my case, I could redesign the schema, so that both types have common base.
But what if I wouldn't have the control over the schema. How could I implement the basic idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议您让这两个类实现一个通用接口,然后强制转换为该接口。我看不出您当前的转换会产生任何效果...
当然,这是假设您可以控制
DepartmentType
和OrganizationType
代码。I suggest you make both classes implement a common interface, and cast to that instead. I can't see that your current casts can have any effect...
This is assuming you have control over the
DepartmentType
andOrganizationType
code, of course.注意:这是当前类层次结构的替代方案。
定义一个名为 UnitTypeBase 的中间类,它从 XmlObject 扩展。
然后
派生 *DepartmentType 和 OrganizationType* 从UnitTypeBase
Note: This is an alternative to the current class hierarchy you have.
Define a intermdeiate class called UnitTypeBase which extends from XmlObject.
Something like
then derive *DepartmentType and OrganizationType* from UnitTypeBase
如果您可以控制架构,则可以定义一个基本抽象类型,其他类型可以从中扩展。我自己没有尝试过,所以我不确定 XmlBeans 将如何处理它。
否则,这是一种解决方法(黑客?),但您可以使用 Commons BeanUtils ,只要您生成的类遵循JavaBean 命名约定。如果这些对象被频繁传递,您可以创建一个包装类以使调用更加具体。
任何问题都可以通过另一层间接解决......除了太多的间接。
If you have control of your schema you can define a base abstract type that your other types can extend from. I haven't tried this myself so I'm not sure how it XmlBeans will handle it.
Otherwise, it's a work-around (hack?) but you could use Commons BeanUtils provided your generated classes follow JavaBean naming convention. If these objects are getting passed around a lot you could create a wrapper class to make the calls a little more concrete.
Any problem can be solved with another layer of indirection.....except too much indirection.
使用 getName 方法创建一个接口,并且 DepartmentType 和 OrganizationType 类都实现该接口。例如 ;
Create an interface with method getName and both classes DepartmentType and OrganizationType implements this interface. Such as ;