代码块、抽象类和接口、内部类、异常

代码块

Person person = new Person();
顺序:静态代码块(只执行一次) --> 代码块 --> 构造方法

public class Person {
    // 2.可以赋初始值
    {
        System.out.println("匿名代码块");
    }
    // 1. 只执行一次
    static {
        System.out.println("静态代码块");
    }
    // 3.
    public Person() {
        System.out.println("构造方法");
    }
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("===========");
        Person person1 = new Person();
        /*
    静态代码块
	匿名代码块
	构造方法
	===========
	匿名代码块
	构造方法
        */
    }
}

抽象类

abstract 修饰

public  abstract class Action {
    //约束,有人帮我们实现
    // abstract ,抽象方法,只有方法名字,没有方法的实现
    public abstract void doSomerhing();
    //1.不能new这个抽象类,只能靠子类去实现它:约束
    //2.抽象类中可以写普通的方法
    //3.抽象方法必须再抽象类中
    //抽象的抽象: 约束
    //  抽象类有构造器
    //存在的意义   抽象出来,提高开发效率
}

接口

interface 修饰
需要有具体的实现类

//抽象思维
    //interface 定义关键字,接口都需要有实现类
public interface UserService {
    //常量   public static final
    int AGE = 99;
    //接口中的所有定义的方法其实都是抽象的 public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);

内部类

Inner就是内部类

public class Outer {
    private int id;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
    }
}

异常

try-catch-finally
try – try监控区域
catch – catch (要捕获异常的类型) 捕获异常
finally(一定会执行的) – 做IO,资源的关闭

 public static void main(String[] args) {
        int a = 1;
        int b = 0;
        // 假设要捕获多个异常:从小到大
        try { //try监控区域
            System.out.println(a/b);
        } catch (Error e) { // catch (要捕获异常的类型) 捕获异常
            System.out.println("Error");
        }catch (Exception e) {
            System.out.println("Exception");
        }catch (Throwable e) {
            System.out.println("Throwable");
        }finally {  //做善后处理,一定会执行
            System.out.println("finally");
        }
        //finally 做IO,资源的关闭
    }

throw – 主动抛出一个 (用于方法内)
throws – 把异常抛给上级来处理 (跟在方法定义后)

 public static void main(String[] args) {
        int a = 1;
        int b = 0;
        // 假设要捕获多个异常:从小到大
        try {
            new Test02().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
        System.out.println("haha");

//            System.out.println(a/b);
    }
    //假设这个方法中,处理不了这个异常,方法上抛出异常
    public void test(int a,int b) throws ArithmeticException{
        if (b == 0){ //throw   throws
            throw new ArithmeticException(); //主动的抛出一个异常,一般用于方法中
        }
    }

自定义异常

//自定义一个异常
public class MyException extends Exception{
    private int detail;
    public MyException(int detail){
        this.detail = detail;
    }
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}

测试自定义异常类

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException ==> "+ e);
        }
    }
    //可能会存在异常的方法
    static void test(int a) throws MyException {
        System.out.println("传递的参数为"+a);
        if (a > 10){
            throw new MyException(a); //抛出
        }
        System.out.println("OK");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值