gpt4 book ai didi

java - Java中不同类型的类使用相同的方法

转载 作者:行者123 更新时间:2023-12-02 10:57:41 25 4
gpt4 key购买 nike

我想将相同的方法应用于不同的类名:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStreamReader reader = new InputStreamReader(request.getInputStream());

String l;
StringBuilder sb1 = new StringBuilder();
BufferedReader br = new BufferedReader(reader);
while (( l = br.readLine()) != null) {
sb1.append(l);
}
InputStream sb2 = new ByteArrayInputStream(sb1.toString().getBytes());
sb1.toString();
if(sb1.indexOf("CheckStatusRequest") != -1) {
CheckStatusRequest checkStatusRequest = (CheckStatusRequest)testXmlToObject(sb2);
System.out.println(checkStatusRequest.getClass());
}else if(sb1.indexOf("CheckStatusResponse") != -1) {
CheckStatusResponse checkStatusResponse = (CheckStatusResponse)testXmlToObject1(sb2);
System.out.println(checkStatusResponse.getClass());
}else if(sb1.indexOf("Consequences") != -1) {
Consequences consequences = (Consequences)testXmlToObject2(sb2);
System.out.println(consequences.getClass());
}else if(sb1.indexOf("SubscriptionRequest") != -1) {
SubscriptionRequest subscriptionRequest = (SubscriptionRequest)testXmlToObject3(sb2);
System.out.println(subscriptionRequest.getClass());
}else if(sb1.indexOf("TerminateSubscriptionRequest") != -1) {
TerminateSubscriptionRequest terminateSubscriptionRequest = (TerminateSubscriptionRequest)testXmlToObject4(sb2);
System.out.println(terminateSubscriptionRequest.getClass());
}
}
public CheckStatusRequest testXmlToObject(InputStream line) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CheckStatusRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CheckStatusRequest object = (CheckStatusRequest)jaxbUnmarshaller.unmarshal(line);
return object;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
public CheckStatusResponse testXmlToObject1(InputStream line) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CheckStatusResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CheckStatusResponse object = (CheckStatusResponse)jaxbUnmarshaller.unmarshal(line);
return object;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}

提出的解决方案有效,但我认为它实现得不好。该方法对于每个类都以相同的方式工作,改变的只是返回对象的类型。基本上我对每个开关盒都有相同的方法。我尝试实现一个解决方案,其中该方法被声明为 Object,并且当调用它时进行了强制转换,但它没有按预期工作,我得到一个 javax.xml.bind.UnmarshalException。你能简化这段代码吗?

最佳答案

您可以通过使用泛型来做到这一点。将您的 testXmlToObject 更改为:

public <T> T testXmlToObject(InputStream line, Class<T> classe)  {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(classe);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
T object = (T) jaxbUnmarshaller.unmarshal(line);
return object;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}

您将该方法调用为 testXmlToObject(line, CheckStatusRequest.class)

关于java - Java中不同类型的类使用相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com