软件设计之Java入门视频(13)
视频教程来自B站尚硅谷:
尚硅谷Java入门视频教程,宋红康java基础视频
相关文件资料(百度网盘)
提取密码:8op3
idea 下载可以关注 软件管家 公众号
学习内容:
该视频共分为1-717部分
 本次内容涉及360-389
 在写代码时,总是需要来回切换界面来看代码要求,这里推荐Snipaste,可以把截图以窗口形式放在屏幕上 
 记录内容:
- 内部类
- 异常
1、内部类
1)Java中允许将一个类A声明在另一个类B中,则类A就是内部类,类B称为外部类
2)内部类的分类:成员内部类(静态、非静态)与局部内部类(方法内、代码块、构造器内)
3)成员内部类
作为外部类的成员:
a)可以调用外部类的结构
b)可以被static修饰
c)可以被四种不同权限修饰(public、缺省、protected、private)
作为类的成员:
a)可以定义属性、方法、构造器
b)可以被final、abstract修饰
package test;
public class InnerClassTest {
    public static void main(String[] args) {
        //创建Dog类(静态成员内部类)
        Person.Dog dog = new Person.Dog();
        dog.show();
        //创建Bird类(非静态成员内部类)
        Person person = new Person();
        Person.Bird bird = person.new Bird();
        bird.sing();
    }
}
class Person{
    int age;
    public void eat(){
        System.out.println("吃饭");
    }
    public void method(){
        //局部内部类
        class AA{
        }
    }
    //静态成员内部类
    static  class Dog{
        int age;
        public void show(){
            System.out.println("叫");
        }
    }
    //非静态成员内部类
    class Bird{
        int age;
        public Bird(){
        }
        public void sing(){
            System.out.println("唱歌");
            Person.this.eat(); //调用外部类的非静态属性
        }
        public void display(int age){
            System.out.println(age); //方法形参
            System.out.println(this.age);//内部类属性
            System.out.println(Person.this.age);//外部类属性
        }
    }
}
2、异常
Error:Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError和OOM。一般不编写针对性的代码进行处理。
Exception:其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如:
空指针访问、试图读取不存在的文件、网络连接中断、数组角标越界
运行时异常:是指编译器不要求强制处置的异常。一般是指编程时的逻辑错误,是程序员应该积极避免其出现的异常。
编译时异常:是指编译器要求必须处置的异常。即程序在运行时由于外界因素造成的一般性异常。编译器要求Java程序必须捕获或声明所有编译时异常
Error举例
public class ErrorTest {
    public static void main(String[] args) {
       // main(args);  栈溢出 StackOverflowError
       Integer[] arr = new Integer [1024*1024*1024] //堆溢出  OOM OutOfMemoryError
       
    }
}
异常处理:抓抛模型
过程1:"抛":程序在正常执行的过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象以后,其后的代码就不再执行。
过程2:“抓”:异常的处理方式
1)try-catch-finally
2) throws
try-catch-finally
1)finally是可选的
2)使用try将可能出现异常代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象类型,去catch中进行匹配
3)一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常处理,一旦处理完成,跳出当前try-catch结构(在没有写finally的情况)
4)catch中的异常类型如果没有子父类关系,声明在上/下不影响
5)catch中的异常类型满足子父类关系,则要求子类一定要声明在父类之上,否则报错unreachalbe
6)常用的异常对象处理的方式:String getMessage();printStackTrace();
7)finally中声明的是一定会被执行的代码。即使catch中又出现异常、try/catch中有return语句等情况
8)像数据库连接、输入输出流、网络编程Socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放。此时的资源释放,就需要声明在finally中。
package test;
import org.junit.Test;
public class ExceptionTest1 {
    @Test
    public void test1(){
        String str = "1223";
        str = "abc";
        try{
            int num = Integer.parseInt(str);
            System.out.println("----1");
        }catch (NumberFormatException e){
            System.out.println("出现数值转换异常");
        }
        System.out.println("----2");
        }
}
throws
1)"throws + 异常类型"写在方法的声明处。指明此方法执行时,可能会抛出的异常类型。一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常类型时,就会被抛出。异常代码后续的代码,就不再执行!
2)try-catch-finally:真正的将异常给处理掉了
3)throw只是将异常抛给了方法的调用者
4) 重写方法异常抛出的规则: 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型
如何选择try-catch与throws
1)如果父类中被重写的方法
没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
2)执行的方法a中,先后又调用了另外的几个方法,这几个方法是递进关系执行的。我们建议这几个方法使用throw的方式进行处理。而执行的方法a可以考虑使用try-catch-finally方式进行处理。
手动抛出异常
package test;
public class StudentTest {
    public static void main(String[] args) {
        Student student = new Student();
        try {
            student.regist(-10);
            System.out.println(student);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
class Student{
    private int id;
    public void regist (int id) throws Exception {
        if(id > 0){
            this.id = id;
        }else {
            //手动抛出异常对象
            // throw new RuntimeException("非法数据"); //此处是运行时异常,所以不会编译报错
            throw new Exception("非法数据");
        }
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}
自定义异常类
1)继承于现有的异常结构
Exception、RuntimeException
2)提供全局常量:serialVersionUID
3)提供重载的构造器
public class MyException extends Exception{
    static final long serialVersionUID = -702321312412L;
    public MyException(){
    }
    public MyException(String msg){
        super(msg);
    }
}
练习

在idea中想要设置main方法的args[]实参:
)
package test;
public class EcmDef {
    public static void main(String[] args) {
        try {
            int i =Integer.parseInt(args[0]);
            int j =Integer.parseInt(args[1]);
            int result = ecm(i,j);
            System.out.println(result);
        }  catch (NumberFormatException e){
            System.out.println("数据类型不一致");
        } catch (ArrayIndexOutOfBoundsException e){
            System.out.println("缺少命令行参数");
        } catch (ArithmeticException e){
            System.out.println("算数异常");
        } catch (EcDef e) {
            System.out.println(e.getMessage());
        }
    }
    public static int ecm (int i , int j) throws EcDef {
        if(i < 0 || j < 0){
            throw new EcDef("分子或分母为负数");
        }
        return i/j;
    }
}




















