public class Person {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("==========");
Person person1 = new Person();
}
}
输出: // 静态代码块 >匿名代码块>构造方法
静态代码块 //只会执行一次
匿名代码块
构造方法
==========
匿名代码块
构造方法
#### 抽象类
约束
定义一些方法,让不同的人实现
接口的方法都是 public abstract开头
接口的常量都是public static final开头
implements可以实现多个接口
必须要重写接口中的方法
异常处理的五个关键字:try、catch、finally、throw、throws
public static void main(String[] args) {
int a = 1;
int b = 0;
//假设要捕获多个异常:从小到大!
//Throwable>Exception
try{//try监控区域
System.out.println(a/b);
}catch (Error e) { // 捕获错误
System.out.println("Error");
}catch (Exception e) { //catch 捕获异常
System.out.println("Exception");
}catch (Throwable t){ //catch 捕获异常
System.out.println("Throwable");
}finally {//处理善后工作,可以不要
System.out.println("fianlly");
}
原文:https://www.cnblogs.com/a806451906/p/14777265.html