栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

抽象类 abstract/接口 interface/异常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

抽象类 abstract/接口 interface/异常

代码块

代码快执行顺序 静态代码块(程序启动时执行一次) 然后时匿名代码块 再然后时构造代码块 普通代码款

抽象类 abstract

用abstract修饰的方法叫抽象方法 只有方法的声明 没有方法体

子类一旦继承了抽象父类 就必须重写抽象类里的所有方法 除非子类也是抽象类

抽象类里面可以有普通方法 但是抽象方法必须在抽象类里

接口 interface 实现 implements 内部类
        Demo20 demo20 = new Demo20();
        Demo20.dd bd = demo20.new dd();
异常
public class Demo22 {
    public static void main(String[] args) {
        int i = 1;
        int j = 0;
        try {
            System.out.println(i/j);
        }catch (Exception E){//捕获出到底是什么异常
            //打印出异常的种类
            E.printStackTrace();
        }
    }
}
public class Demo21 {
    public static void main(String[] args) {
        int i = 1;
        int j = 0;
        try {//监控是否出现异常
            System.out.println(i/j);
        }catch (ArithmeticException e){//捕获异常出现异常执行
            System.out.println("出现异常");
        }catch(Throwable throwable){
            System.out.println("出现异常");
        }
        finally {//出不出现异常都执行
            System.out.println("");
        }

    }
}
public class Demo23 {
    public static void main(String[] args) {
        Demo23 demo23 = new Demo23();
        try{//捕获异常
            demo23.Test(1,0);
        }catch(ArithmeticException arithmeticException){
            System.out.println("抛出异常");
        }


    }
    //向上抛出异常
    public void Test(int i ,int j) throws ArithmeticException{
        if (j == 0){
            //捕获异常
            throw new ArithmeticException();
        }
    }
}
自定义异常
public class MyException extends Exception{
    private  int e = 0 ;
    public MyException(int a){
        this.e = a;
    }

    @Override
    public String toString() {
        return "MyException{" +
                "e=" + e +
                '}';
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号