java笔试题目(帮忙回答以下) 第一题:两个对象:1,"xyz";2:指向“xyz”的引用对象s 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String s = new String("xyz");创建了几个String Object?2个编程写一个Singleton出来。(或者用自然语言讲出你的思路)class xx{static t=nullprivate xx(){};public xx create(){ if(xx==null) t=new xx(); return xx;}} 当然是一个对象了,new了一次而已.单例实在太简单,用饿汉模式就不会有双重check的问题了.在JBuilder些一下,然后查看UML视图,打印出来我还真记不得这些url,不过不难,连接池就是一个连接的池,其实应该比较简单,和一般的缓冲区差不多 关于Singleton:有几种形式: 如:第一种形式:饿汉Singletonpublic class Singleton { private static Singleton x=new Singleton(); private Singleton(){} public static Singleton getInstance(){ return x; }}第二种形式: 懒汉Singletonpublic class Better{ private static Better x=null; private Better(){} public static Better getInstance(){ if(x==null) x=new Better(); return x; }//lazy instantiation}但是请注意,上述两种Singleton不能继承,因为Constructor为private,这样就引出了第三种:登记式Singletonpublic class RegSingleton { static private HashMap m_registry = new HashMap(); static { RegSingleton x = new RegSingleton(); m_registry.put(x.getClass().getName(), x); } protected RegSingleton() { } public static RegSingleton getInstance(String name) { if (name == null) { name = "RegSingleton"; } if (m_registry.get(name) == null) { try { m_registry.put(name, Class.forName(name).newInstance()); } catch (Exception e) { System.out.println("Error happened."); } } return (RegSingleton)(m_registry.get(name)); } public String about() { return "Hello, I am RegSingleton."; } } RegSingletonChild.java import java.util.HashMap; public class RegSingletonChild extends RegSingleton { public RegSingletonChild() { } static public RegSingletonChild getInstance() { return (RegSingletonChild) RegSingleton.getInstance("RegSingletonChild"); } public String about() { return "Hello, I am RegSingletonChild."; } } 对象应该是两个。“xx”为一个,new后又有一个。 一个java类的疑问 applet问题 3个郁闷的表 如何配置MCKOI1.0.3数据库啊 运行的时候为什么说找不到系统文件啊? static一点小问题 一个简单的对象申明和定义的问题 跪求:农历转换程式 - 寻求java的代码 你的举手之劳@我的燃眉之急 用java写的程序能生成exe文件么? 三个入门级问题 正则表达式如何匹配表格?
2个编程写一个Singleton出来。(或者用自然语言讲出你的思路)
class xx
{
static t=null
private xx(){};
public xx create()
{
if(xx==null)
t=new xx();
return xx;
}
}
如:
第一种形式:饿汉Singleton
public class Singleton {
private static Singleton x=new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return x;
}}第二种形式: 懒汉Singleton
public class Better{
private static Better x=null;
private Better(){}
public static Better getInstance(){
if(x==null)
x=new Better();
return x;
}//lazy instantiation
}
但是请注意,上述两种Singleton不能继承,因为Constructor为private,这样就引出了
第三种:登记式Singletonpublic class RegSingleton
{
static private HashMap m_registry = new HashMap(); static
{
RegSingleton x = new RegSingleton();
m_registry.put(x.getClass().getName(), x);
} protected RegSingleton()
{ } public static RegSingleton getInstance(String name)
{
if (name == null)
{
name = "RegSingleton";
} if (m_registry.get(name) == null)
{
try
{
m_registry.put(name, Class.forName(name).newInstance());
}
catch (Exception e)
{
System.out.println("Error happened.");
} } return (RegSingleton)(m_registry.get(name));
} public String about()
{
return "Hello, I am RegSingleton.";
}
} RegSingletonChild.java
import java.util.HashMap; public class RegSingletonChild extends RegSingleton
{
public RegSingletonChild()
{
} static public RegSingletonChild getInstance()
{
return (RegSingletonChild)
RegSingleton.getInstance("RegSingletonChild");
} public String about()
{
return "Hello, I am RegSingletonChild.";
}
}
“xx”为一个,new后又有一个。