您的位置:首页 > 其它

几道笔试题

2015-11-26 02:48 405 查看
1.单例模式

饿汉模式:

特点:加载类慢,获取对象速度快

public class Singleton{
//1.创建私有构造方法
private Singleton(){
}<span>		</span>
private static Singleton instance=new Singleton();//2.创建类的唯一实例
<span>	</span>//3.提供一个用于获取实例的方法
public static Singleton getInstance(){
return instance;
}
}
懒汉模式:

特点:加载类快,获取对象慢

public class Singleton{
//1.创建私有构造方法
private Singleton(){
}
private static Singleton instance;//2.声明类的唯一实例
//3.提供一个用于获取实例的方法
public static Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
}


2.下面代码输出结果为

public class Test1 {
public static void changeStr(String str) {
str += "welcome";
}

public static void main(String[] args) {
String str = "1234";
changeStr(str);
System.out.println(str);
}
}


答案为:1234      
System.out.println(str);  //输出的只是main 方法中的str


3.下面运行结果为

class HelloA {

public HelloA() {
System.out.println("HelloA");
}

{ System.out.println("I'm A class"); }

static { System.out.println("static A"); }

}

public class HelloB extends HelloA {
public HelloB() {
System.out.println("HelloB");
}

{ System.out.println("I'm B class"); }

static { System.out.println("static B"); }

public static void main(String[] args) {
     new HelloB();
   }

}


输出:

static A

static B

Hello A

I'm A class

Hello B 

I'm B   class

先执行静态语句  在自上而下

4.判断一个对象是否是一个已知类的对象,关键字   instanceof  



t = 3;
System.out.println(t instanceof Integer);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: