几道笔试题

来源:互联网 发布:bu大都会学院知乎 编辑:程序博客网 时间:2024/05/01 19:25

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);


0 0