1、面试题java面试题大全-代码与编程题代码与编程题135、写一个Singleton出来Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。一般Singleton模式通常有几种种形式:第一种形式: 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类初始化时实例话,通过一个public的getInstance方法获取对它的引用,继而调用其中的方法。public class Singleton private Singleton() /在自己内部定义自己一个实例,是不是很奇怪? /注意这是private 只供内部调
2、用 private static Singleton instance = new Singleton(); /这里提供了一个供外部访问本class的静态方法,可以直接访问 public static Singleton getInstance() return instance; 第二种形式: public class Singleton private static Singleton instance = null;public static synchronized Singleton getInstance() /这个方法比上面有所改进,不用每次都进行生成对象,只是第一次 /使用时生
3、成实例,提高了效率!if (instance=null)instancenew Singleton();return instance; 其他形式:定义一个类,它的构造函数为private的,所有方法为static的。一般认为第一种形式要更加安全些 136、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?答:父类:package test;public class FatherClass public FatherClass() System.out.println(FatherClass Create); 子类:package test;import test.FatherCl
4、ass;public class ChildClass extends FatherClass public ChildClass() System.out.println(ChildClass Create); public static void main(String args) FatherClass fc = new FatherClass(); ChildClass cc = new ChildClass(); 输出结果:C:java test.ChildClassFatherClass CreateFatherClass CreateChildClass Create 137、内
5、部类的实现方式?答:示例代码如下:package test;public class OuterClass private class InterClass public InterClass() System.out.println(InterClass Create); public OuterClass() InterClass ic = new InterClass(); System.out.println(OuterClass Create); public static void main(String args) OuterClass oc = new OuterClass()
6、; 输出结果:C:java test/OuterClassInterClass CreateOuterClass Create再一个例题:public class OuterClass private double d1 = 1.0; /insert code here You need to insert an inner class declaration at line 3. Which two inner class declarations are valid?(Choose two.) A. class InnerOne public static double methoda()
7、 return d1; B. public class InnerOne static double methoda() return d1; C. private class InnerOne double methoda() return d1; D. static class InnerOne protected double methoda() return d1; E. abstract class InnerOne public abstract double methoda(); 说明如下:一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。 故 A、B 错二.静态内部
8、类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。 故 D 错三.非静态内部类的非静态成员可以访问外部类的非静态变量。 故 C 正确四.答案为C、E 138、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示? 答:Server端程序:package test;import .*;import java.io.*; public class Server private ServerSocket ss; private Socket socket; private BufferedReader i
9、n; private PrintWriter out; public Server() try ss=new ServerSocket(10000); while(true) socket = ss.accept(); String RemoteIP = socket.getInetAddress().getHostAddress(); String RemotePort = :+socket.getLocalPort(); System.out.println(A client come in!IP:+RemoteIP+RemotePort); in = new BufferedReader
10、(new InputStreamReader(socket.getInputStream(); String line = in.readLine(); System.out.println(Cleint send is : + line); out = new PrintWriter(socket.getOutputStream(),true); out.println(Your Message Received!); out.close(); in.close(); socket.close(); catch (IOException e) out.println(wrong); publ
11、ic static void main(String args) new Server(); ;Client端程序:package test;import java.io.*;import .*; public class Client Socket socket; BufferedReader in; PrintWriter out; public Client() try System.out.println(Try to Connect to 127.0.0.1:10000); socket = new Socket(127.0.0.1,10000); System.out.printl
12、n(The Server Connected!); System.out.println(Please enter some Character:); BufferedReader line = new BufferedReader(new InputStreamReader(System.in); out = new PrintWriter(socket.getOutputStream(),true); out.println(line.readLine(); in = new BufferedReader(new InputStreamReader(socket.getInputStrea
13、m(); System.out.println(in.readLine(); out.close(); in.close(); socket.close(); catch(IOException e) out.println(Wrong); public static void main(String args) new Client(); ; 139、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?答:用插入法进行排序代码如下package test;import java.util.*;class InsertSor
14、t ArrayList al; public InsertSort(int num,int mod) al = new ArrayList(num); Random rand = new Random(); System.out.println(The ArrayList Sort Before:); for (int i=0;inum ;i+ ) al.add(new Integer(Math.abs(rand.nextInt() % mod + 1); System.out.println(al+i+=+al.get(i); public void SortIt() Integer tem
15、pInt; int MaxSize=1; for(int i=1;i=(Integer)al.get(MaxSize-1).intValue() al.add(MaxSize,tempInt); MaxSize+; System.out.println(al.toString(); else for (int j=0;j=tempInt.intValue() al.add(j,tempInt); MaxSize+; System.out.println(al.toString(); break; System.out.println(The ArrayList Sort After:); fo
16、r(int i=0;ial.size();i+) System.out.println(al+i+=+al.get(i); public static void main(String args) InsertSort is = new InsertSort(10,100); is.SortIt(); 140、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。 答:代码如下:package test; class
17、SplitString String SplitStr; int SplitByte; public SplitString(String str,int bytes) SplitStr=str; SplitByte=bytes; System.out.println(The String is:+SplitStr+;SplitBytes=+SplitByte); public void SplitIt() int loopCount; loopCount=(SplitStr.length()%SplitByte=0)?(SplitStr.length()/SplitByte):(SplitS
18、tr.length()/Split Byte+1); System.out.println(Will Split into +loopCount); for (int i=1;i=loopCount ;i+ ) if (i=loopCount) System.out.println(SplitStr.substring(i-1)*SplitByte,SplitStr.length(); else System.out.println(SplitStr.substring(i-1)*SplitByte,(i*SplitByte); public static void main(String a
19、rgs) SplitString ss = new SplitString(test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103,4); ss.SplitIt(); 141、JAVA多线程编程。 用JAVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。 希望大家补上,谢谢 142、可能会让你写一段Jdbc连Oracle的程序,并实现数据查询.答:程序如下:package hello.ant;import java.sql.*;public class jdbc String dbUrl=jdbc:oracle:thin:127.0.0.1:15
20、21:orcl; String theUser=admin; String thePw=manager; Connection c=null; Statement conn; ResultSet rs=null; public jdbc() try Class.forName(oracle.jdbc.driver.OracleDriver).newInstance(); c = DriverManager.getConnection(dbUrl,theUser,thePw); conn=c.createStatement(); catch(Exception e) e.printStackTr
21、ace(); public boolean executeUpdate(String sql) try conn.executeUpdate(sql); return true; catch (SQLException e) e.printStackTrace(); return false; public ResultSet executeQuery(String sql) rs=null; try rs=conn.executeQuery(sql); catch (SQLException e) e.printStackTrace(); return rs; public void clo
22、se() try conn.close(); c.close(); catch (Exception e) e.printStackTrace(); public static void main(String args) ResultSet rs; jdbc conn = new jdbc(); rs=conn.executeQuery(select * from test); try while (rs.next() System.out.println(rs.getString(id); System.out.println(rs.getString(name); catch(Excep
23、tion e) e.printStackTrace(); 143、ORACLE大数据量下的分页解决方法。一般用截取ID方法,还有是三层嵌套方法。 答:一种分页方法=intPageCount) nextPage=intPageCount; upPage = currentPage-1; if (upPage=1) upPage=1; rs.close(); sql=select * from tables; rs=DBLink.executeQuery(sql); i=0; while(i/输出内容/输出翻页连接合计:/第一页a href=List.jsp?page=上一页% for(int j
24、=1;j a href=list.jsp?page= a href=List.jsp?page=下一页a href=List.jsp?page=最后页 144、用jdom解析xml文件时如何解决中文问题?如何解析?答:看如下代码,用编码方式加以解决package test;import java.io.*;public class DOMTest private String inFile = c:people.xml; private String outFile = c:people.xml; public static void main(String args) new DOMTest
25、(); public DOMTest() try javax.xml.parsers.DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = builder.newDocument(); org.w3c.dom.Element root = doc.createElement(老师); org.w3c.dom.Element wang = doc.createElement(王); org.w
26、3c.dom.Element liu = doc.createElement(刘); wang.appendChild(doc.createTextNode(我是王老师); root.appendChild(wang); doc.appendChild(root); javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, gb2312); transformer.se
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1