1、3代码与编程题代码与编程题135、写一个Singleton出来Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。一般Singleton模式通常有几种种形式:第一种形式:定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类初始化时实例话,通过一个public的getInstance方法获取对它的引用,继而调用其中的方法。publicclassSingletonprivateSingleton()/在自己内部定义自己一个实例,是不是很奇怪?/注意这是private只供内部调用privatestaticSingle
2、toninstance=newSingleton();/这里提供了一个供外部访问本class的静态方法,可以直接访问publicstaticSingletongetInstance()returninstance;第二种形式:publicclassSingletonprivatestaticSingletoninstance=null;publicstaticsynchronizedSingletongetInstance()/这个方法比上面有所改进,不用每次都进行生成对象,只是第一次/使用时生成实例,提高了效率!if(instance=null)instancenewSingleton();
3、returninstance;其他形式:定义一个类,它的构造函数为private的,所有方法为static的。一般认为第一种形式要更加安全些136、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?答:父类:package test;public class FatherClass public FatherClass()System.out.println(FatherClass Create);子类:package test;import test.FatherClass;public class ChildClass extends FatherClasspublic Chi
4、ldClass()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、内部类的实现方式?答:示例代码如下:package test;public class OuterClassprivate c
5、lass InterClasspublic 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();输出结果:C:java test/OuterClassInterClass CreateOuterClass Create再一个例题:p
6、ublic 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() return d1; B. public class InnerOne static double methoda() return d1
7、; 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 错二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。 故 D 错三.非静态内部类的非静态成员可以
8、访问外部类的非静态变量。 故 C 正确四.答案为C、E 138、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示? 答:Server端程序:package test;import .*;import java.io.*; public class Serverprivate ServerSocket ss;private Socket socket;private BufferedReader in;private PrintWriter out;public Server()tryss=new ServerSocket(10000);wh
9、ile(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(new InputStreamReader(socket.getInputStream();String line = in.readLine();System.o
10、ut.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);public static void main(String args)new Server();Client端程序:package test;import java.io.*;import .
11、*; public class ClientSocket socket;BufferedReader in;PrintWriter out;public Client()trySystem.out.println(Try to Connect to 127.0.0.1:10000);socket = new Socket(127.0.0.1,10000);System.out.println(The Server Connected!);System.out.println(Please enter some Character:);BufferedReader line = new Buff
12、eredReader(new InputStreamReader(System.in);out = new PrintWriter(socket.getOutputStream(),true);out.println(line.readLine();in = new BufferedReader(new InputStreamReader(socket.getInputStream();System.out.println(in.readLine();out.close();in.close();socket.close();catch(IOException e)out.println(Wr
13、ong);public static void main(String args)new Client(); 139、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?答:用插入法进行排序代码如下package test;import java.util.*;class InsertSortArrayList al;public InsertSort(int num,int mod)al = new ArrayList(num);Random rand = new Random();System.out.println(T
14、he 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 tempInt;int MaxSize=1;for(int i=1;i=(Integer)al.get(MaxSize-1).intValue()al.add(MaxSize,tempInt);MaxSize+;System.out.println(al.toSt
15、ring(); 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:);for(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.S
16、ortIt();140、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。 答:代码如下:package test; class SplitStringString SplitStr;int SplitByte;public SplitString(String str,int bytes)SplitStr=str;SplitByte=bytes;System.out.println(The String is:+
17、SplitStr+;SplitBytes=+SplitByte);public void SplitIt()int loopCount; loopCount=(SplitStr.length()%SplitByte=0)?(SplitStr.length()/SplitByte):(SplitStr.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.subs
18、tring(i-1)*SplitByte,SplitStr.length(); else System.out.println(SplitStr.substring(i-1)*SplitByte,(i*SplitByte);public static void main(String args) SplitString ss = new SplitString(test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103,4);ss.SplitIt(); 141、JAVA多线程编程。 用JAVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。 希望大家补
19、上,谢谢 142、可能会让你写一段Jdbc连Oracle的程序,并实现数据查询.答:程序如下:package hello.ant;import java.sql.*;public class jdbcString dbUrl=jdbc:oracle:thin:127.0.0.1:1521:orcl;String theUser=admin;String thePw=manager;Connection c=null;Statement conn;ResultSet rs=null;public jdbc() try Class.forName(oracle.jdbc.driver.Oracle
20、Driver).newInstance(); c = DriverManager.getConnection(dbUrl,theUser,thePw); conn=c.createStatement();catch(Exception e)e.printStackTrace();public boolean executeUpdate(String sql)tryconn.executeUpdate(sql);return true;catch (SQLException e)e.printStackTrace();return false;public ResultSet executeQu
21、ery(String sql)rs=null;tryrs=conn.executeQuery(sql);catch (SQLException e)e.printStackTrace();return rs;public void close()tryconn.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)
22、;trywhile (rs.next()System.out.println(rs.getString(id);System.out.println(rs.getString(name);catch(Exception e)e.printStackTrace(); 143、ORACLE大数据量下的分页解决方法。一般用截取ID方法,还有是三层嵌套方法。 答:一种分页方法=intPageCount) nextPage=intPageCount; upPage = currentPage-1; if (upPage=1) upPage=1; rs.close(); sql=select * from
23、 tables; rs=DBLink.executeQuery(sql); i=0; while(i/输出内容/输出翻页连接合计:/第一页a href=List.jsp?page=上一页% for(int j=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 DOMTestprivate String inFile
24、= c:people.xml;private String outFile = c:people.xml;public static void main(String args) new DOMTest(); 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
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1