先来两道小题目
1,
public class ABC{
publicvoid abc(Object aa) {
System.out.println(“object...”);
}
publicvoid abc(String aa) {
System.out.println(“aaaaaaaaaaaaa”);
}
publicstatic void main(String[] args) {
ABCa = new ABC();
a.abc(null);
}
}
答:调用的是abc(String aa)方法,Java是较小优先原则,因为String是Object的子类,所以调用的是String。
2,面试题:
public class BCD{
publicstatic void main(String[] args) {
for(;;)
Object o = new Object();
}
}
问:红色部分报错,为什么?
在for循环外面加上大括号,(不是死循环,也会出现这种情况)
public class BCD{
publicstatic void main(String[] args) {
for(;;){
Object o = new Object();
}
}
}
就不报错了,为什么?
3,通过urlConnection的方式获取服务器提供的服务。
public class UrlConnDemo { @Test public void getAll() throws Exception{ URL url = new URL("http://192.168.1.254:2345/hello"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type","text/xml;charset=UTF-8"); con.setDoInput(true);//可以从服务器上读取消息 con.setDoOutput(true);//可以向服务器与参数 //读取xml文件,根据xml文件中的信息来访问服务器提供的服务, //比如,通过a.xml文件可以知道访问那个方法 InputStream in = UrlConnDemo.class.getResourceAsstream("a.xml"); ByteArrayOutputStream bt= new ByteArrayOutputStream(); int len = 0; byte[] by = new byte[1024]; while(-1!=(len=in.read(by))){ bt.write(by,len); } in.close(); OutputStream out = con.getoutputStream(); out.write(bt.toByteArray()); out.close(); //从服务器上读取数据 InputStream is = con.getInputStream(); // BufferedReader br = new BufferedReader(new InputStreamReader(is)); // String line = null; // while(null!=(line=br.readLine())){ // System.err.println(line); // } SAXReader sax = new SAXReader(); Document dom = sax.read(is); List<Element> users = dom.selectNodes("//return"); for(Element el:users){ String id = el.elementText("id"); String nm = el.elementText("name"); System.err.println("id:"+id+","+nm); } is.close(); con.disconnect(); } }
<it:Envelope xmlns:it="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <it:Body> <q0:getAll /> //调用getAll()方法 </it:Body> </it:Envelope>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。