如何向上转换和使用子特定方法

发布于 2024-10-29 17:10:36 字数 880 浏览 0 评论 0原文

我遇到以下问题:

//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 & ,您的方法似乎是最合乎逻辑的。 组织类型。不幸的是,这些对象是由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

做个ˇ局外人 2024-11-05 17:10:36

我建议您让这两个类实现一个通用接口,然后强制转换为该接口。我看不出您当前的转换会产生任何效果...

public interface NamedElement
{
    String getName();
}

...

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

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

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...

public interface NamedElement
{
    String getName();
}

...

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

This is assuming you have control over the DepartmentType and OrganizationType code, of course.

久随 2024-11-05 17:10:36

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

定义一个名为 UnitTypeBase 的中间类,它从 XmlObject 扩展
然后

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 ... 

Note: This is an alternative to the current class hierarchy you have.

Define a intermdeiate class called UnitTypeBase which extends from XmlObject.
Something like

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

then derive *DepartmentType and OrganizationType* from 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 ... 
少钕鈤記 2024-11-05 17:10:36

如果您可以控制架构,则可以定义一个基本抽象类型,其他类型可以从中扩展。我自己没有尝试过,所以我不确定 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");
    }
}

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

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.

<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>

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.

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

Any problem can be solved with another layer of indirection.....except too much indirection.

故人爱我别走 2024-11-05 17:10:36

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

Create an interface with method getName and both classes DepartmentType and OrganizationType implements this interface. Such as ;

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文