我有一个子类孩子,它派生自基类父。我想在Base Class的属性周围添加一个Wrapper。
Parent.java
10-12 16:59:37.427 25539-25539/com.gpslocmex.locmex E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.util.AndroidRuntimeException: { what=0 when=-1ms } This message is already in use.
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:285)
at android.os.Handler.sendMessageAtTime(Handler.java:473)
at android.os.Handler.postAtTime(Handler.java:285)
at com.google.maps.api.android.lib6.d.cm.c(Unknown Source)
at com.google.maps.api.android.lib6.d.cm.b(Unknown Source)
at com.google.maps.api.android.lib6.d.et.a(Unknown Source)
at com.google.android.gms.maps.internal.j.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at com.gpslocmex.locmex.Mapa_activity.renovarmarcador(Mapa_activity.java:218)
at com.gpslocmex.locmex.Mapa_activity$4$1.handleMessage(Mapa_activity.java:161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Child.java
package test;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Parent {
int a;
int b;
String name;
public Parent() {
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
的Output.xml
package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Child extends Parent {
private String foo;
public Child() {
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
但我想要这样的事情:
<child>
<a>1</a>
<b>3</b>
<name>name</name>
<foo>foo</foo>
</child>
我希望有人可以帮助我。
答案 0 :(得分:2)
你想做的事情是可行的,但可能不值得麻烦。您需要为Child
课程实施XmlAdapter,
package test;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public final class ChildAdapter extends
XmlAdapter<PrintedType, Child> {
@Override
public Child unmarshal(PrintedType v) throws Exception {
Child ret = new Child();
ret.setFoo(v.foo);
ret.setA(v.parent.getA());
ret.setB(v.parent.getB());
ret.setName(v.parent.getName());
return ret;
}
@Override
public PrintedType marshal(Child v) throws Exception {
PrintedType ret = new PrintedType();
ret.parent = v;
ret.foo = v.getFoo();
return ret;
}
}
以及生成您想要的XML表示的值类型:
package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="child")
@XmlAccessorType(XmlAccessType.FIELD)
public class PrintedType {
String foo;
Parent parent;
}
然后,如果你想编组一个Child
的实例作为根元素(而不是在其他类中有一个Child
成员字段,并且编组该类),你实际上有首先将Child
转换为PrintedType
(https://stackoverflow.com/a/11967459/4854749)。所以样本测试类可能是
package test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws JAXBException {
Child child = new Child();
child.setA(1);
child.setB(3);
child.setName("name");
child.setFoo("foo");
JAXBContext jc = JAXBContext.newInstance(PrintedType.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
PrintedType ret = new PrintedType();
ret.parent = child;
ret.foo = child.getFoo();
marshaller.marshal(ret, System.out);
}
}
您确定这对您要解决的问题有意义吗?我不知道你的背景,但问问自己哪种听起来更合适:Child
是 Parent
,或Child
包含 Parent
?如果是后者,则可以通过取消继承和使用合成来使事情变得更加简单(使Child
包含一个Parent
字段)。那么你就不需要适配器类了,而Child
会以你想要的方式编组。