java从零到项目实战(七)

java内部类及常见异常问题

内部类

内部类算是java写类的高阶玩法,目前占时不需要掌握,看见能知道就足够。
在这里插入图片描述

成员内部类

在类中再定义一个类

//外部类
public class Outer {public void out(){System.out.println("这是外部类方法");}//成员内部类public class Inner{public void in(){System.out.println("这是内部类方法");}}
}

访问内部类,需要外部类来进行实例化

public class test {public static void main(String[] args) {//外部类实例化Outer oo = new Outer();//内部类实例化//通过实例化的外部类来实例化内部类Outer.Inner ii = oo.new Inner();oo.out();ii.in();}
}

同时,内部类本身可以获得外部类的私有属性和方法
在这里插入图片描述

静态内部类

static关键字所修饰的内部类

//外部类
public class Outer {public void out(){System.out.println("这是外部类方法");}//静态内部类public static class Inner{public void in(){System.out.println("这是静态内部类方法");}}
}

由于static是在外部类加载时一同加载的,所以若用静态内部类访问外部类的非静态属性和方法或报错,因为静态类部类加载时外部类的非静态属性和方法还未加载。
在这里插入图片描述

局部内部类

写在外部类方法中的类

package oop.Demo12;//外部类
public class Outer {public void method(){//局部内部类class Inner{}}}

匿名内部类

定义在外部类之外的类

public class test {public static void main(String[] args) {//普通初始化类Apple app = new Apple(); //将实例化的对象赋值给了app//匿名内部类初始化,可以是没有名字的初始化,即不用将实例对象保存到变量中new Apple().go(); }
}//匿名内部类
class Apple{public void go(){System.out.println("匿名内部类");}
}

注意:一个java文件中,只能有一个public class,但是可以有对个class。
在这里插入图片描述

异常机制(exception)

异常也是类,对异常的处理也是对类的处理

什么是异常

在这里插入图片描述
在这里插入图片描述

异常体系架构

在这里插入图片描述

超类即指所有类的祖宗类,相当于object类
在这里插入图片描述

Error是错误,严格来说不是异常,通常与代码编写者本身无关,可能是编译环境、版本等硬件问题
在这里插入图片描述

运行异常通常是与代码编写有关了。如果是非运行异常,那就需要先保证代码正确,编译通过才能运行。

Java异常处理机制

在这里插入图片描述

通常当异常出现时,程序往往会停止运行。

public class test {public static void main(String[] args) {int a = 10;int b = 0;System.out.println(a/b);//除数不能为0,运行时会出现异常System.out.println("这里出错了");}
}

在这里插入图片描述

如果说,除数不能为0这个异常我们在编写时就已经知道,但我们并不着急去改,同时希望程序能继续往下执行的话,就需要对异常进行相关处理。

处理异常

  1. 捕获异常套餐:try + catch 或 try + catch + finally(自动生成快捷键:选中需要捕获异常的代码ctrl + alt + t)

    其中,try、catch、finally均为代码块。try代码块中放入的是需要判断是否异常的内容;catch代码块负责捕获自己所选择的异常,如果捕获成功,则会执行代码块中的相关处理;finally代码功能在于善后处理,不论是否捕获成功异常,最终都会执行的部分。

    示例代码:

    public class test {public static void main(String[] args) {int a = 10;int b = 0;try{ //try监控区域System.out.println(a/b);}catch (ArithmeticException f){ //捕获异常System.out.println("程序出现异常,变量b不能为0");}finally {  //处理善后工作System.out.println("finally");}System.out.println("这里出错了");}
    }

    这样就能通过捕获异常,让程序继续运行下去而不会中断
    在这里插入图片描述

    注意:catch可以有很多个,捕获异常的类型按照编写代码的顺序需从上至下递增,否则会报错(具体大小关系参考异常体系结构)
    在这里插入图片描述

  2. 抛出异常: throw、throws

    在方法中出现异常时,有两种解决办法,一种是throw就地正法,一种是通过throws声明异常类型往外抛,交给调用此方法的对象去处理。

    throw就地正法

    代码运行到throw会自动停止,此时通过捕获异常的方法直接在方法中处理异常

    public class test {public static void main(String[] args) {new test().tt(10,0);System.out.println("meishime");}public void tt (int a,int b){//抛出异常if (b == 0){//方法体内自己解决try {//想要抛出一个Error异常throw new Error();} catch (Error error) {System.out.println("异常已解决");}}}
    }

在这里插入图片描述

throws声明异常类型往外抛

方法本身不打算处理这个异常,在定义方法名时声明抛出类型,然后交给调用者处理

public class test {public static void main(String[] args) {//调用时处理throws抛出的异常try {new test().tt(10,0);} catch (Error e) {System.out.println("异常已解决");}System.out.println("meishime");}public void tt (int a,int b)throws Error{//声明该方法可能会抛出Error异常//抛出异常if (b == 0){//想要抛出一个Error异常throw new Error();}}
}

在这里插入图片描述

自定义异常

在这里插入图片描述

  1. 创建自定义异常类

    创建一个类用于继承Exception类。其功能是当接收参数被判定为异常时打印异常信息

    //自定义的异常类
    public class MyException extends Exception{//接收参数变量public int num;//构造器public MyException(int a) {this.num = a;//传递参数赋值给num}//toString 异常参数的信息打印@Overridepublic String toString() {return "MyException{" +"num=" + num +'}';}
    }
    
  2. 使用自定义异常MyException

    例如,定义一个可能产生异常的方法,当传递的参数为大于10的数时,抛出异常。

    public class test {//可能出现异常的方法public static void say(int a){if (a>10){throw new MyException(a);//抛出异常 }}public static void main(String[] args) {say(11);}
    }
  3. 在抛出异常的方法中处理或将异常向外抛出给调用者处理的方式中二选一进行处理

    在抛出异常的方法中处理(throw + try~catch):

    public class test {public static void say(int a){if (a>10){//在抛出异常的方法中直接处理try {throw new MyException(a);} catch (MyException e) {System.out.println("异常是" + e);}}}public static void main(String[] args) {say(11);}
    }

    将异常向外抛出给调用者处理(throws + try~catch):

    public class test {//将异常向外抛出给调用者处理public static void say(int a) throws MyException{if (a>10){throw new MyException(a);}}public static void main(String[] args) {//在调用时处理异常try {say(11);} catch (MyException e) {System.out.println("异常是" + e);}}
    }

在这里插入图片描述
在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部