静态代码块
格式:static{}
作用:静态数据初始化(系统启动是需要初始化的静态代码)
构造代码块
格式:{}
静态代码块执行一次,构造代码块只要创建对象就会被执行
使用案例:
因为进入发牌时系统只发一次牌,所以考虑用静态代码块
package staticDaiMaKuai; import java.util.ArrayList; /* 使用代码块进行扑克牌的初始化发牌 使用静态代码块 */ public class pan { public static ArrayList<String> card = new ArrayList<>();//定义静态的集合 static { card.add("大王"); card.add("小王"); String[] k = {"♠","♣","♥","♦"}; String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; for (int i = 0; i < k.length; i++) { for (int j = 0; j < numbers.length; j++) { card.add(k[i]+numbers[j]); } } } public static void main(String[] args) { System.out.println(card); } }
ps.学习记录,观看黑马教程后总结