一、内部类
1、将一个类定义在另一个类的里面,对里面那个类就成为内部类(内置类,嵌套类)2、访问特点、访问规则:
(1)、内部类可以直接访问外部类中的成员,包括私有成员
(2)、而外部类要访问内部类中的成员必须要建立内部类的对象
(3)、之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用, 格式类名.this
(3)、外部类.内部类 变量名 = new 外部类().new 内部类();
二、静态内部类
1、当内部类在成员位置上时,就可以被成员修饰符所修饰
(1)、private:将内部类在外部类中进行封装
(2)、static:内部类就具备了静态的特性,当内部类别static修饰后,只能直接访问外部类中的static成员,出现了访问局限
(3)、访问被static 修饰的内部类,访问非静态 new 外部类.内部类().方法
(3)、访问被static 修饰的内部类,访问静态 new 外部类.内部类.方法
2、访问格式
(1)、当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,可以直接建立内部类对象
(2)、外部类.内部类 变量名 = new 外部类().new 内部类();
3、主意:
(1)、当内部类中定义了静态成员,该内部类必须是static
(2)、当外部类中的静态方法,访问内部类时,内部类也必须是 static
三、内部类定义原则
1、类是用来描述事物的,当描述事物时,事物的内部还有事物,该事物用内部类来描述,因为内部事物在使用外部事物的内容。
2、隐藏内部类,提供相应接口
3、内部类定义在局部,时
(1)、不可以被成员修饰符修饰
(2)、可以直接访问外部类中的成员,因为还持有外部类中的引用,但是不可以访问它所在的局部的变量,只能访问被final修饰的局部变量。
- class Outer{
- int x = 3;
- void method(){
- System.out.println("outer:"+x);
- }
- class Inner{
- //int x = 4;
- void function(){
- //int x = 4;
- System.out.println("inner:"+x);
- // System.out.println("inner:"+this.x);
- // System.out.println("inner:"+Outer.this.x);
- }
- }
- }
- public class Demo{
- public static void main(String[] args){
- // Outer.Inner in = new Outer().new Inner();
- // in.function();
- }
- }
四、匿名内部类
1、匿名内部类其实就是内不累的简写形式
2、定义匿名内部类的前提
(1)、内部类必须是继承一个类或者实现接口
3、老例子
- abstract class AbsDemo{
- abstract void show();
- }
- class Outer{
- int x = 3;
- void method(){
- System.out.println("outer:"+x);
- }
- class Inner extends AbsDemo{
- //int x = 4;
- void show(){
- //int x = 4;
- System.out.println("inner:"+x);
- // System.out.println("inner:"+this.x);
- // System.out.println("inner:"+Outer.this.x);
- }
- }
- public void function(){
- new Inner().show();
- }
- }
- public class Demo{
- public static void main(String[] args){
- new Outer().function();
- }
- }
4、匿名内部类的格式:new 父类或者接口(){定义子类的内容}
5、其实匿名内部类就是一个匿名子类对象,而且这个对象有点胖,也可以理解为,带内容的对象
6、新例子
- abstract class AbsDemo{
- abstract void show();
- }
- class Outer{
- int x = 3;
- void method(){
- System.out.println("outer:"+x);
- }
- public void function(){
- new AbsDemo(){
- void show(){
- System.out.println("x="+x);
- }
- }.show();
- }
- }
- public class Demo{
- public static void main(String[] args){
- new Outer().function();
- }
- }
7、匿名颞部类中定义的方法最好不要超过3个
8、例子
- // abstract class Inter{
- // void method();
- // }
- interface Inter{
- void method();
- }
- class Test{
- // static class Inner implements Inter{
- // public void method(){
- // System.out.println("method run");
- // }
- // }
- static Inter function(){
- //return new Inner();
- return new Inter(){
- public void method(){
- System.out.println("mehthod run");
- }
- };
- }
- }
- public class Demo{
- public static void main(String[] args){
- Test.function().method();
- }
- }
五、异常
1、异常的体系
(1)、error
通常出现重大问题如:运行的类不存在或者内存溢出等
不缩写针对代码对其处理
(2)、exception
在运行是运行出现的一起情况,都可以通过 try catch finally
2、exception和error的子类都是以父类名作为后缀
3、异常:就是程序在运行时出现不正常情况
4、异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。其实就是java对不正常情况进行描述,后的对象体系那。
5、对于问题的划分:两种
(1)、一种是严重的问题,java通过error类进行描述
a、对于error,一般不编写针对性的代码对其进行处理
(2)、对于非严重的,java通过exception类进行描述
b、对于exception可以使用针对想的
(3)、无论error或者exception都具有一些共性内容,比如:不正常情况的信息,引发原因等。
6、Throwable
(1)、Error
(2)、Exception
7、例子
- class ExceptionDemo{
- int div(int a,int b){
- return a/b;
- }
- }
- public class Demo{
- public static void main(String[] args){
- ExceptionDemo ed = new ExceptionDemo();
- int x = ed.div(4,0);
- //x = ed.div(4,1);//编译不报错,但是运行报错 Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionDemo.div(Demo.java:3) at Demo.main(Demo.java:9)
- System.out.println("x="+x);
- System.out.println("over");
- }
- }
六、异常try-catch
1、异常的处理
try{
需要被检测的代码
}catch(){
处理异常的代码:(处理方式)
}finally{
一定会执行的语句
}
2、例子
- class ExceptionDemo{
- int div(int a,int b){
- return a/b; //产生了 ArithmeticException,并把问题封装成了一个对象
- }
- }
- public class Demo{
- public static void main(String[] args){
- ExceptionDemo ed = new ExceptionDemo();
- try{
- int x = ed.div(4,0); //new ArithmeticException()
- System.out.println("x="+x); //因为上边已经发生了异常,所以这一步不会再执行
- }catch(Exception e){ //Exception e = ArithmeticException();
- System.out.println("除零了");
- System.out.println(e.toString());//异常名称:异常信息
- System.out.println(e.getMessage());//by zero
- System.out.println(e.printStackTrace());//异常名称,异常信息,异常出现的位置
- //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。打印异常的堆栈的跟踪信息。
- }
- System.out.println("over");
- }
- }
3、对捕获到的异常进行常见方法操作。
(1)、String getMessage():获取异常信息
(2)、toString():返回此 throwable 的简短描述
七、异常声明throws
1、在类的函数上 声明 throws Exception,在对象在调用函数时,必须捕捉或者声明抛出
2、声明异常时,建议声明更具体的异常,这样处理可以更具体
3、对方声明几个异常,就对应有几个catch块
(1)、如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面
4、建立在进行catch处理是,catch中一定要定义具体处理方式,不要简单定义一句,e.printStackTrace(),也不要简单的就书写出一条语句
5、
- class ExceptionDemo{
- int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{ //再功能上通过 throws 的关键字声明了该功能有可能会出现问题,可以声明多个异常
- int[] arr = new int[a];
- System.out.println(arr[3]);//虽然有多个异常,但是当前面有异常时,该方法块内,异常后面的就不会运行
- return a/b;
- }
- }
- public class Demo{
- public static void main(String[] args){
- ExceptionDemo ed = new ExceptionDemo();
- try{
- int x = ed.div(3,0);
- System.out.println("x="+x);
- }catch(ArithmeticException e){
- System.out.println("除零了");
- }catch(ArrayIndexOutOfBoundsException e){
- System.out.println("角标越界了");
- }
- System.out.println("over");
- }
- }
八、自定义异常
1、因为项目中会出现特有的问题,而这些问题并未被java所表述并封装对象,所以对于这些特有的问题可以按照java的对问题的封装思想。将特有的问题,进行自定义的异常封装
2、当在函数内部出现了 throw 抛出异常对象,那么就必须要给对应的处理动作
(1)、要么在内部 try catch 处理,
(2)、要么在函数上声明让调用者处理
3、一般情况下,函数内出现异常,函数上需要声明
4、定义异常信息,因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,那么就可以直接通过 getMessage 方法获取自定义的异常信息
5、自定义类必须是继承 Exception 类。
6、为什么要继承exception
(1)、异常体系有一个特点:因为异常类和异常对象都要被抛出,他们都具备可抛性
(2)、这个可抛性是 Throwable 这个体系中独有特点
(3)、只有这个体系中的类和对象才可以被 throws 和 throw 操作
7、例子
/*
需求:在本程序中,对于除数是-1,也视为是错误
- */
- class FuShuException extends Exception{
- private int value;
- FuShuException(String msg,int value){
- super(msg);//定义异常信息,因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,那么就可以直接通过 getMessage 方法获取自定义的异常信息
- this.value = value;
- }
- public int getValue(){
- return value;
- }
- }
- class ExceptionDemo{
- int div(int a,int b) throws FuShuException{ //函数内出现异常,函数上需要声明
- if(b<0){
- throw new FuShuException("除数为负数了",b);
- }
- return a/b;
- }
- }
- public class Demo{
- public static void main(String[] args){
- ExceptionDemo ed = new ExceptionDemo();
- int x = 0;
- try{
- x = ed.div(3,-1);
- }catch(FuShuException e){
- System.out.println(e.toString());
- System.out.println("该负数是:"+e.getValue());
- }
- System.out.println("x="+x);
- System.out.println("结束");
- }
- }
十、throw和throws的区别
1、throws 使用在函数上,throw 使用在函数内
2、throws 后面跟的异常类,可以跟多个,用逗号隔开,throw 后面跟的是异常对象。
十一、RuntimeException
1、Exception中有一个特殊的子类异常, RuntimeException 运行异常
2、如果在函数内抛出异常,函数上可以不用声明,编译一样通过
3、如果在函数上声明了该异常,调用者可以不用进行处理。编译一样通过
4、之所以不用在函数上声明,是因为不需要让调用者处理。当该异常发生,希望程序停止,因为在运行时,出现了无法继续的运算情况,希望停止程序后,对代码进行修正。
5、自定义异常时,如果该异常的发生,无法再继续进行运算,就让自定义异常继承 RuntimeException
6、对于异常,分两种
(1)、编译时被检测的异常(如Exception)
(2)、编译时不被检测的异常(运行时异常,RuntimeException以及子类)
十二、练习
- /*
- 毕老师用电脑上课
- 上课中出现的问题
- 电脑蓝屏。电脑冒烟
- 对问题进行描述,封装成对象
- 可是当冒烟发生后,出现讲课进度无法继续。
- 出现了讲师的问题,课时计划无法完成。
- */
- class LanPingException extends Exception{
- LanPingException(String message){
- super(message);
- }
- }
- class MaoYanException extends Exception{
- MaoYanException(String message){
- super(message);
- }
- }
- class noPlanException extends Exception{
- noPlanException(String message){
- super(message);
- }
- }
- class Computer{
- private int state = 3;//1为正常状态,
- // Computer(){
- // }
- public void run() throws LanPingException,MaoYanException{
- if(state==2){
- throw new LanPingException("蓝屏了");
- }else if(state==3){
- throw new MaoYanException("冒烟了");
- }
- System.out.println("电脑运行");
- }
- public void reset(){
- state = 1;
- System.out.println("电脑重启");
- }
- }
- class Teacher{
- private String name;
- private Computer cmpt;
- Teacher(String name){
- this.name = name;
- cmpt = new Computer();
- }
- public void prelect() throws noPlanException{
- try{
- cmpt.run();
- }catch(LanPingException e){
- cmpt.reset();
- }catch(MaoYanException e){
- test();
- throw new noPlanException("课时无法继续,"+e.getMessage());
- }
- System.out.println(name+"老师上课");
- }
- public void test(){
- System.out.println("做练习");
- }
- }
- class Demo{
- public static void main(String[] args){
- Teacher t = new Teacher("毕老师");
- try{
- t.prelect();
- }catch(noPlanException e){
- System.out.println(e.getMessage());
- System.out.println("换老师或者换电脑或者放假");
- }
- }
- }