努力学习打卡Day15

static工具类:

package d2_static_method;

import java.util.Random;

//工具类
public class yoe {
    /**
     * 由于工具类无需创建对象,所以把其构造器私有化会显得很专业
     */
    private yoe(){
    }
    public static String CreateVerifyCode(int n ){
        //开发一个验证码
        String code = "";
        Random r = new Random();
        String data = "abcdefghijklmnopqrstvuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (int i = 0; i < n; i++) {
            //获取随机索引对应的字符
            int index = r.nextInt(data.length());
            code += data.charAt(index);
        }
        return code;
    }
}

实例:(数组的工具类设计) 

package d2_static_method2;
//完成数组的工具类设计
public class ArrayUtil {
    //私有构造器
    private ArrayUtil(){
    }

    //工具方法:静态方法
    public static String toString(int[] arr){
        //1,一些校验
        if(arr == null){
            return null;
        }
        //2,拼接内容并返回
        String result = "[";
        for (int i = 0; i < arr.length; i++) {
            result += (i == arr.length-1?arr[i]:arr[i]+",");
        }
        result += "]";
        return result;
    }
}
package d2_static_method2;

public class Test {
    public static void main(String[] args) {
        int[] arr = null;
        int[] arr1 = {};
        int[] arr2 = {1, 2, 3, 4};
        System.out.println(ArrayUtil.toString(arr));
        System.out.println(ArrayUtil.toString(arr1));
        System.out.println(ArrayUtil.toString(arr2));
    }
}

工具类可以节省代码的重复,当需要用时直接调用即可。

static 代码块:

 

package d3_static_code;

import java.util.ArrayList;

public class StaticDemo1 {
    //静态资源
    public static String schoolName;
    public static ArrayList<String> cards = new ArrayList<>();
    //静态代码块,有static修饰,属于类,与类优先加载一次,自动触发执行
    //作用:可以用于初始化静态资源
    static{
        System.out.println("======静态代码块被触发执行了======");
        schoolName = "yoe";
        cards.add("3");
        cards.add("4");
    }

    public static void main(String[] args) {
        System.out.println("---main方法执行---");
        System.out.println(schoolName);
    }



}
package d3_static_code;

public class StaticDemo2 {
    public StaticDemo2(){
        System.out.println("===无参构造器被触发执行===");
    }
    //实例代码块(构造代码块),属于对象,每次构造对象是,都会触发一次执行(了解就行,很少用)
    {
        System.out.println("===实例代码块被触发执行===");
    }
    public static void main(String[] args) {
        StaticDemo2 s1 = new StaticDemo2();
        StaticDemo2 s2 = new StaticDemo2();
    }
}

实例:(模拟斗地主游戏游戏开始前的系统提前准备一副牌)

package d3_static_code;

import java.util.ArrayList;
//模拟游戏启动前,初始化54张牌数据
public class Static_Test {
    //1:定义一个静态的集合,这样这个集合只加载一个,因为当前房间只需要一副牌
    public static ArrayList<String> cards = new ArrayList<>();
    //2:在程序真正运行main方法前,把54张牌放进去,游戏后续可以直接使用
    static{
        //3:正式做牌,放到集合中去
        //a:定义一个数组存储全部点数:类型确定了,个数确定了(用数组存)
        String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        //b:定义一个数组存储全部花色:类型确定,个数确定
        String[] colors = {"♥", "♠", "♦", "♣"};
        //c:遍历点数
        for (int i = 0; i < sizes.length; i++) {
            //d:遍历花色
            for (int j = 0; j < colors.length; j++) {
                String card = sizes[i]+colors[j];
                cards.add(card);
            }
        }
        //e:单独加入大小王
        cards.add("小🃏");
        cards.add("大🃏");
    }
    public static void main(String[] args) {
        System.out.println("新牌:"+ cards);
    }
}

static单例模式:

 

1:饿汉单例:

package d4_static_singleinstance;
//使用饿汉单例实现单例类
public class SingleInstance {
    //2:饿汉单例是在获取对象前,对象已经提前准备好了一个
    //这个对象只能是一个,所以定义静态成员变量记住
    public static SingleInstance instance = new SingleInstance();

    //1:必须把构造器私有化。
    private SingleInstance(){};
}
package d4_static_singleinstance;
//目标:理解饿汉单例的设计步骤
public class Test {
    public static void main(String[] args) {
        SingleInstance s1 = SingleInstance.instance;
        SingleInstance s2 = SingleInstance.instance;
        System.out.println(s1 == s2);
    }


}

2:懒汉单例:

 

package d4_static_singleinstance2;

public class SingleInstance2 {
    //2:定义一个静态成员变量负责存储一个对象,只加载一次,只有一份
    //注:这里不能new一个对象出来因为懒汉单例需要在用时创建,并且只能创建一次
    private static SingleInstance2 instance;

    //3:提供一个方法,对外返回单例对象
    //注意:最好私有化,这样可以避免给别人挖坑,防止别人直接“.instance”
    public static SingleInstance2 getInstance(){
        if(instance == null)//当他没有时才创建
        {
            instance = new SingleInstance2();
        }
        return instance;
    }
    //1:私有化构造器
    private SingleInstance2(){
    }

}
package d4_static_singleinstance2;
//目标:掌握懒汉单例的设计,理解其思想
public class Test {
    public static void main(String[] args) {
        SingleInstance2 s1 = SingleInstance2.getInstance();
        SingleInstance2 s2 = SingleInstance2.getInstance();
        System.out.println(s1 == s2);
    }
}

前几天摆烂了,今天继续。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值