繁体   English   中英

JAXB:在超类中声明的字段注释在子类中有所不同

[英]JAXB: Annotations of field declared in superclass differ in subclasses

我将尝试用一个例子来解释这个问题:

基础:

public abstract class Base {
   protected Foo foo;
}

Derived1:

//@SomeXMLAnnotations
public class Derived1 extends Base {
   //Here i would like to define annotations for foo
   @XmlElements({
       @XmlElement(name = "foo1",   type = Foo1.class),
       @XmlElement(name = "foo2",   type = Foo2.class)
   })
   //@AnyAnnoations..
   //protected Foo foo;
}

Derived2的:

//@SomeXMLAnnotations
public class Derived2 extends Base {
   //Here i would like to define annotations for foo too
   //But they will differ from the ones defined in Derived1
   @XmlElements({
       @XmlElement(name = "foo3", type = Foo3.class),
       @XmlElement(name = "foo4", type = Foo4.class)
   })
   //@AnyAnnoations..
   //protected Foo foo;
}

@XmlElements注释只是一个示例。 它也应该与任何其他注释一起使用。

我知道我可以遮盖超类foo字段,但是我认为这不是解决此问题的正确方法。

那么在Java(使用JAXB)中是否可以重写/向超类中声明的字段添加注释?

“丑陋”的解决方案:

//@SomeXMLAnnotations
public class Derived1 extends Base {

   //@AnyAnnoations..
   public Foo getFoo() { 
       return this.foo 
   }

   protected Foo setFoo(Foo foo) { 
       this.foo = foo; 
   }
}

覆盖/添加属性并对其进行注释似乎可行。 如果没有在任何地方使用getter或setter,那将是非常丑陋的,因为这必须在每个子类中发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM