面试 1笔记

单例模式

// 饿汉式  第一种
public class Singleton1 {
    private Singleton1(){}
    public static final Singleton1 INSTANCE = new Singleton1();
}
public enum  Singleton2 {
    INSTANCE;
}
import java.io.IOException;
import java.util.Properties;

//静态方法加载
public class Singleton3 {
    private Singleton3(String 单例){}
    public  static Singleton3 INSTANCE;
    static {
        Properties properties = new Properties();
        try {
            properties.load(Singleton3.class.getClassLoader().getResourceAsStream("路径文件"));

            INSTANCE = new Singleton3(properties.getProperty("单例"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

// 懒汉式  线程 不安全
public class Singleton4 {
    private Singleton4(){}
    private static Singleton4 INSTANCE;
    public static Singleton4 getInstance(){
        if (INSTANCE==null){
            INSTANCE = new Singleton4();
        }
        return INSTANCE;
    }

}

 

public class Singleton5 {
    private Singleton5 (){}
    private static Singleton5 INSTANCE ;
    public static Singleton5 getINSTANCE(){
        synchronized (Singleton5.class){
            if (INSTANCE==null){
                INSTANCE = new Singleton5();
            }
        }
        return INSTANCE ;
    }
}
/*
在内部类比诶加载和初始化时,才创建INSTANCE的实例对象
静态内部类不会自动随着外部类的加载和初始化而初始化  它是要加载和初始化
因为是在内部类加载和初始化 创建  因此线程是安全的
 */
public class Singleton6 {
    private Singleton6(){}
    private static class Inner{
        public  static  final Singleton6 INSTANCE = new Singleton6();
    }

    public static Singleton6 getInstance (){
        return  Inner.INSTANCE;
    }
}

 

answer:  5 1 10 6  9 3 2  9  8  7 

9 3 2 9 8 7  ;

 

 

答案解析 

 

 

 

 

 

 

 

 

 public int f (int n){
        if (n<=2&&n>0) return n;
        return f(n-1)+f(n-2);
    }
​
    public static int f2 (int n){
        int  i = 1;
        int  j = 2 ;
        int sum = 2;
        if (n<=2&&n>0) return n;
        int counter = 3;
        while (counter<=n){
            j = i;  // 记录 count 1的值
            i = sum;//    count 2 的值
            sum = i+j;    //获得  count3 的值
            counter++;
        }
        return sum;
    }

 

 

6

 

加上this 指的就是 上面的成员变量 ,不加this指的是  局部变量 

 

 

 

7

 

 

 

8

 

 

 

 

 

 

 

Redis

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值