Java学习Day06 各种类 接口 异常

Java学习Day06 各种类 接口 异常

抽象类

//抽象类 修饰词 extends 单继承
//接口可以多继承
public abstract class Action {

    //约束 只有方法的约束名字 没有方法实现
    public abstract void doSomething();
//1 抽象类不能实例化也就是不能new 只能靠子类实现
//2 抽象方法必须在抽象类中 抽象类里可以有非抽象方法
    public void go(){

    }

    public Action() {
    }
//抽象类 有没有构造器 有
    //为什么要用抽象类 提高开发效率
}
//抽象类的所有方法 都必须实现它的方法 除非子类也是抽象方法 那就让子子类重写
public class A extends Action{
    @Override
    public void doSomething() {

    }
}

接口

//接口
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);
}
//接口作用
//约束
//定义一些方法 让不同的人实现
//public abstract
//接口不能实例化 接口中没有构造方法
//implements可以实现多个接口
//必须重写接口中的所有方法
//接口UserService 继承多个
//实现接口的类 必须重写接口的方法
public class UserServiceImp1 implements UserService,TimeService {
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }
    @Override
    public void time() {

    }
}
public interface TimeService {

    void time();
}
//TimeService和UserService 都被继承

内部类

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.out();
        Outer.Inner inner = outer.new Inner();//内部类的调用
        inner.in();
        inner.getID();
    }

}
//一些不推荐类的写法
public class Outer {

    private int id = 10;
    public void out(){
        System.out.println("外部类");
    }
    public class Inner{
        public void in(){
            System.out.println("内部类");
        }
        public void getID(){
            System.out.println(id);
        }
        //内部类可以调用private属性
    }
    public void method(){
        class A{

        }
    }
}
//一个java类中有多个class类不能有多个public class类
//静态内部类 只是在类加载是就加载和普通内部类只有时间先后的关系
//静态类不能调用 非静态类原因同上

异常

/*
* error 可以预料到
* error 大多错误是java虚拟机运行错误 和代码操作无关
*        一般是灾难性的错误
*
* exception 异常一般预料不到
* Runtime(非运行时异常) 分类 一般是编码者出问题
*
* 非运行时异常
*
* 一般是可以处理的异常
* */
public class Demo01 {
    public static void main(String[] args) {
        new Demo01().A();

    }
    //这里就是无限循环的异常
    public void A(){
        B();
    }
    public void B(){
        A();
    }
}
//try  catch finally finally可以不用
public class Test {
    public static void main(String[] args) {
        int a= 1;
        int b= 0;
        try {//监控区域
            System.out.println(a/b);
            new Test().a();
        }catch (Error e){//捕获异常 想要捕获的异常类型 捕获类型从小到大
            System.out.println("程序异常,变量b不能为0");
        }
        catch (Throwable t){
            System.out.println("Throwable");
        }
        finally {//处理善后工作
            System.out.println("结束");
        }

        try {
            new Test().test(1,2);
        } catch (ArithmeticException e) {
            throw new RuntimeException(e);
        } finally {
        }

//io 需要关闭 finally就可以关闭io流
    }

    public void a(){b();}
    public void b(){a();}

    public void test(int a,int b) throws ArithmeticException{

            if (b == 0) {//主动抛出异常 throw throws 一般在方法中使用
                throw new ArithmeticException();
            }
            System.out.println(a/b);
    }

}
public class Test02 {
    public static void main(String[] args) {
        int a= 1;
        int b= 0;

        try {
            if (b == 0) {//主动抛出异常 throw throws
                throw new ArithmeticException();
            }
            System.out.println(a/b);//ctrl + alt + t 自动包裹
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        }

    }

}

自定义的异常

//自定义的异常类
public class MyException extends Exception{
    //传递数字>10
    private  int detail;

    public MyException(int a){
        this.detail = a;
    }
//alt + insert ToString
    //打印异常信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}
public class MyTest {

    static void test(int a) throws MyException {
        System.out.println("传递的参数为:");

        if (a > 10) {
            throw new MyException(a);
        }
        System.out.println("Ok");
    }

    public static void main(String[] args) {
        try {
            test(1);
        } catch (MyException e) {
            System.out.println("MyException"+e);
        } finally {
        }
    }

}
//当然java有很多异常 一般不需要自定义异常、
//异常的分类

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值