13 内部类
13.1内部类
定义:将一个类定义在另一个类中,内部的类称为内部类。
特点:
- 内部类可以直接访问外部类中的成员,包括私有成员;
- 外部类要访问内部类中的成员必须要建立内部类的对象;
- 内部类定义在成员位置上,可以被private、static成员修饰符修饰。被static修饰的内部类只能访问外部类中的静态成员。
例1:
/*
* 内部类的访问规则:
* 1、内部类可以直接访问外部类中的成员,包括私有。
* 之所以可以直接访问外部类中的成员,是因为内部类中持有一个外部类的引用,格式:外部类名.this
* 2、外部类要访问内部类,必须建立内部类对象。
*/
class Outer{
private int x=3;
class Inner{
int x = 4;
void function(){
int x = 6;
System.out.println("Inner:"+Outer.this.x);
}
}
void method(){
Inner in = new Inner();
in.function();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
//直接访问内部类中的成员
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
输出结果:Inner:3
当内部类被static修饰后,只能直接访问外部类中的static成员;
在外部其他类中,如何直接访问static内部类的非静态成员?
new Outer.Inner().function();在外部其他类中,如何直接访问static内部类的静态成员?
Outer.Inner.function();
ps:当内部类中定义了静态变量,该内部类必须是static的;
当外部类中的静态方法访问内部类时,内部类也必须是static的。
例2
class Outer{
private static int x=3;
static class Inner{
void show(){
System.out.println("show:"+x);
}
}
}
public class InnerClassDemo {
public static void main(String[] args) {
//如果内部类是静态的,相当于一个外部类
Outer.Inner in = new Outer.Inner();
in.show();
}
}
输出结果:show:3
内部类定义在局部时,不可以被成员修饰符修饰;
可以直接访问外部类中的成员,因为还持有外部类中的引用;
13.2 匿名内部类
匿名内部类:
- 匿名内部类是内部类的简写格式;
- 定义匿名内部类的前提:内部类必须是继承一个类或者实现接口;
- 匿名内部类的格式:new 父类或者接口()(定义子类的内容);
- 匿名内部类就是一个匿名子类对象。可理解为带内容的对象。
例1:
abstract class Inner{
abstract void show();
}
class Outer{
int x=3;
void method(){
new Inner(){
void show(){
System.out.println("show:"+x);
}
}.show();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
new Outer().method();
}
}
运行结果:show:3
例2:
interface Inner{
void show1();
void show2();
}
class Outer{
public void method(){
Inner in = new Inner(){
public void show1(){
System.out.println("show1:");
}
public void show2(){
System.out.println("show2:");
}
};
in.show1();
in.show2();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
new Outer().method();
}
}
运行结果:show1:
show2:
匿名内部类中定义的方法最好不要超过3个
例3
interface Inner{
void method();
}
class Outer{
static Inner function(){
return new Inner(){
public void method(){
System.out.println("show1:");
}
};
}
}
public class InnerClassDemo {
public static void main(String[] args) {
//Outer.function():Outer类中有一个静态的方法function。
//.method():function方法运算后的结果为一个对象,而且是一个Inner类型对象
//因为只有是Inner类型的对象,才可以调用method方法。
Outer.function().method();
//相当于下方语句
//Inner in=Outer.function();
//in.method();
}
}
14 异常
异常:程序运行时出现的不正常情况;
由来:问题也是现实中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。其实就是java对不正常情况进行描述后的对象体现。描述不正常情况的类,称为异常类。
异常体系特点:异常体系中所有的类以及建立的对象都具备可抛性;也就是说可以被throw和throws关键字所操作,只有异常体系具备这个特点。
异常划分:
Throwable
|-Error
|-Exception
|-RuntimeException
- 严重的,java通过Error类进行描述,是由jvm抛出的严重性问题,对于Error一般不编写针对性的代码对其进行处理;
- 非严重的,java通过Exception类进行描述,对于Exception可以使用针对性的处理方式进行处理。
14.1 异常的处理
java提供了特有的语句进行处理
try
{
需要被检测的代码;
}
catch()
{
处理异常的代码(处理方式);
}
finally
{
一定会执行的语句;
}
处理过程:
- try中检测异常会将异常对象传递给catch,catch捕获到异常进行处理;
- finally内通常用来关闭资源。例如:数据库资源,IO资源等;
- finally代码块只有一种情况不会被执行,就是之前执行System.exit(0)。
例1
class Demo{
int div(int a,int b){
return a/b;
}
}
public class ExceptionDemo {
public static void main(String[] args) {
Demo d = new Demo();
try{
int x = d.div(2, 0);
System.out.println("x="+x);//Exception e = new ArithmeticException();
}catch(Exception e){
System.out.println("分母不能为0");
System.out.println(e.getMessage());// /by zero
System.out.println(e.toString());//异常名称:异常信息
e.printStackTrace();//异常名称,异常信息,异常出现的位置
//其实jvm默认的异常处理机制,就是在调用printStackTrace
//打印异常的堆栈跟踪信息
}
System.out.println("Over!");
}
}
运行结果:
针对例1中一般类Demo的编写者会通过throws Exception抛出可能出现的异常,在使用这个类时就不会出现使用者无法判断该类是否可能出现异常的情况,如例2。
例2
class Demo{
int div(int a,int b)throws Exception{//在功能上通过throws的关键字声明该功能有可能出现的问题
return a/b;
}
}
public class ExceptionDemo {
public static void main(String[] args){
Demo d = new Demo();
int x;
try {
x = d.div(2, 0);
System.out.println("x="+x);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Over!");
}
}
运行结果:
14.2对多异常的处理:
- 声明异常时,建议声明更为具体的异常,这样的处理更为具体;
- 对方声明几个异常,就对应几个catch块;不要定义多余catch块,如果多个catch块中的异常出现继承关系,父类异常catch块放在最下方;
- 进行catch处理时,catch中一定要定义具体处理方式,不能简单的e.printStackTrace();也不要简单一条书写语句,项目开发一般会生成错误日志保存在硬盘中,后续进行处理。
例3:
class Demo{
int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{
int[] arr = new int[a];
System.out.println(arr[4]);
return a/b;
}
}
public class ExceptionDemo {
public static void main(String[] args){
Demo d = new Demo();
int x;
try {
x = d.div(3,0);
System.out.println("x="+x);
} catch(ArithmeticException e1) {
System.out.println(e1.toString());
System.out.println("分母不能为0");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.toString());
System.out.println("角标越界了");
}
System.out.println("Over!");
}
}
运行结果:
14.3 自定义异常
在项目中,会出现特定的问题,而这些问题并未被java所描述并封装对象;对于这些特有的问题,进行自定义异常封装。
当函数内部出现了throw抛出了异常对象,那么就必须要给出对应的处理动作;要么在内部try catch处理,要么在函数上声明让调用者处理;
一般情况在,函数内部出现异常,函数上需要声明。
自定义异常必须是自定义类继承Exception;
继承Exception的原因:
异常体系有一个特点:因为异常类和异常对象都被抛出,他们都具备可抛性,这个可抛性是Throwable这个体系中的独有特点。
只有这个体系中的类和对象才可以被throw和throws操作。
例4:
class FuShuException extends Exception{
private int value;
/* FuShuException(){//构造体
super();
}*/
FuShuException(String msg,int value){//构造体
super(msg);
this.value=value;
}
public int getValue(){
return value;
}
}
class Demo{
int div(int a,int b)throws FuShuException{
if(b<0)
throw new FuShuException("appear",b);
return a/b;
}
}
public class ExceptionDemo {
public static void main(String[] args){
Demo d = new Demo();
int x;
try {
x = d.div(3,-1);
System.out.println("x="+x);
} catch(FuShuException e) {
System.out.println(e.toString());
System.out.println("除数不能为负数:"+e.getValue());
}
System.out.println("Over!");
}
}
运行结果:
throw和throws的区别:
- throws使用在函数上,throw使用在函数内;
- throws后面跟的异常类,可以跟多个,用逗号隔开,throw后面跟的是异常对象。
异常的注意事项:
- RuntimeException以及其子类如果在函数中被throw抛出,可以不用在函数上声明。
- 子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的子类。
- 如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
简单说:子类覆盖父类只能抛出父类的异常或者子类的子集。
如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try。
对于异常分两种:
- 编译时被检测的异常(非RuntimeException以及其子类),必须在类名后加throws的标识;
- 编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
异常在子父类覆盖中的体现:
- 子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者该异常的子类;
- 如果父类方法抛出多个异常,子类在覆盖该方法时,只能抛出父类异常的子集;
- 如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子类方法发生了异常,就必须要进行try处理,而不能抛。
例5:
class NoValueException extends RuntimeException{
NoValueException(String msg){
super(msg);
}
}
interface Shape{
void getArea();
}
class Rec implements Shape{
private int len,wid;
Rec(int len,int wid) {//throws NoValueException
if(len<=0||wid<=0)
throw new NoValueException("出现非法值");
this.len=len;
this.wid=wid;
}
public void getArea(){
System.out.println(len*wid);
}
}
class Circle implements Shape{
private int radius;
public static final double PI = 3.14;
Circle(int radius){
if(radius<=0)
throw new NoValueException("非法");
this.radius = radius;
}
public void getArea(){
System.out.println(radius*radius*PI);
}
}
public class ExceptionDemo {
public static void main(String[] args){
//try {
Rec r = new Rec(1,2);
r.getArea();
Circle c = new Circle(-8);
/* } catch (NoValueException e) {
System.out.println(e.toString());
}*/
System.out.println("Over!");
}
}
运行结果:
14.4 总结
异常体系特点:异常体系中所有的类以及建立的对象都具备可抛性;也就是说可以被throw和throws关键字所操作,只有异常体系具备这个特点。
异常划分:
Throwable
|-Error
|-Exception
|-RuntimeException
throw和throws的区别:
- throws使用在函数上,throw使用在函数内;
- throws后面跟的异常类,可以跟多个,用逗号隔开,throw后面跟的是异常对象。
当函数内容有throw抛出异常对象,并未进行try处理,必须要在函数上声明,都在编译失败;
注意:RuntimeException除外,即如果函数内抛出RuntimeException异常,函数上可以不用声明。
如果函数声明了异常,调用者需要进行处理,处理方法可以throws可以try。
异常有两种:
- 编译时被检测异常
该异常在编译时,如果没有处理(没有抛也没有try),编译失败。该异常被标识,代表可以被处理。
- 运行时异常(编译时不被检测)
在编译时,不需要被处理,编译器不检查。该异常发生,建议不处理,让程序停止,需要对代码进行修正。
异常处理语句:
try{
需要被检测代码;
}catch(){
处理异常的代码;
}
finally{
一定会执行的代码;
}
三种格式:
1
try{
需要被检测代码;
}catch(){
处理异常的代码;
}
2
try{
需要被检测代码;
}catch(){
处理异常的代码;
}
finally{
一定会执行的代码;
}
3
try{
需要被检测代码;
}
finally{
一定会执行的代码;
}
ps:
- finally中定义通常为关闭资源代码,因为资源必须释放。
- finally代码块只有一种情况不会被执行,就是之前执行System.exit(0)。
自定义异常:
定义类继承Exception或者RuntimeException
1. 为了让该自定义类具备可抛性;
2. 让该类具备操作异常的共性方法。
当要定义自定义异常的信息时,可以使用父类已经定义好的功能。异常信息传递给父类的构造函数。
class MyException extends Exception{
MyException(String msg){
super(msg);
}
}
异常好处:
- 将问题进行封装;
- 将正常流程代码和问题处理代码相分离,方便阅读。
异常处理原则:
- 处理方式有两种:try或者throws;
- 调用到抛出异常的功能时,抛出几个,处理几个;即出现一个try对应多个catch;
- 多个catch,父类的catch放在最末端;
- catch内需要定义针对性的处理方式,不要简单定printStackTrace,输出语句,也不要不写;
当捕获到异常,本功能处理不了时,可以继续在catch中抛出
try{ throw new AException(); }catch(AException e){ throw e; }
如果该异常处理不了,但并不属于该功能出现的异常,可以将异常转化后,再抛出和该功能相关的异常;
或者异常可以处理,当需要将异常产生的和本功能相关的问题提供出去,当调用者知道,并处理,也可以将捕获异常处理后,转换新的异常。
try{
throw new AException();
}catch(AException e){
//对AException处理
throw new BException();
}