读书人

Java基础-第九天 面向对象错误

发布时间: 2013-02-24 17:58:56 作者: rapoo

Java基础---第九天 面向对象异常

一、内部类

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修饰的局部变量。
[java] view plaincopy
  1. class Outer{
  2. int x = 3;
  3. void method(){
  4. System.out.println("outer:"+x);
  5. }
  6. class Inner{
  7. //int x = 4;
  8. void function(){
  9. //int x = 4;
  10. System.out.println("inner:"+x);
  11. // System.out.println("inner:"+this.x);
  12. // System.out.println("inner:"+Outer.this.x);
  13. }
  14. }
  15. }
  16. public class Demo{
  17. public static void main(String[] args){
  18. // Outer.Inner in = new Outer().new Inner();
  19. // in.function();
  20. }
  21. }


四、匿名内部类
1、匿名内部类其实就是内不累的简写形式
2、定义匿名内部类的前提
(1)、内部类必须是继承一个类或者实现接口
3、老例子
[java] view plaincopy
  1. abstract class AbsDemo{
  2. abstract void show();
  3. }
  4. class Outer{
  5. int x = 3;
  6. void method(){
  7. System.out.println("outer:"+x);
  8. }
  9. class Inner extends AbsDemo{
  10. //int x = 4;
  11. void show(){
  12. //int x = 4;
  13. System.out.println("inner:"+x);
  14. // System.out.println("inner:"+this.x);
  15. // System.out.println("inner:"+Outer.this.x);
  16. }
  17. }
  18. public void function(){
  19. new Inner().show();
  20. }
  21. }
  22. public class Demo{
  23. public static void main(String[] args){
  24. new Outer().function();
  25. }
  26. }


4、匿名内部类的格式:new 父类或者接口(){定义子类的内容}
5、其实匿名内部类就是一个匿名子类对象,而且这个对象有点胖,也可以理解为,带内容的对象
6、新例子
[java] view plaincopy
  1. abstract class AbsDemo{
  2. abstract void show();
  3. }
  4. class Outer{
  5. int x = 3;
  6. void method(){
  7. System.out.println("outer:"+x);
  8. }
  9. public void function(){
  10. new AbsDemo(){
  11. void show(){
  12. System.out.println("x="+x);
  13. }
  14. }.show();
  15. }
  16. }
  17. public class Demo{
  18. public static void main(String[] args){
  19. new Outer().function();
  20. }
  21. }


7、匿名颞部类中定义的方法最好不要超过3个
8、例子
[java] view plaincopy
  1. // abstract class Inter{
  2. // void method();
  3. // }
  4. interface Inter{
  5. void method();
  6. }
  7. class Test{
  8. // static class Inner implements Inter{
  9. // public void method(){
  10. // System.out.println("method run");
  11. // }
  12. // }
  13. static Inter function(){
  14. //return new Inner();
  15. return new Inter(){
  16. public void method(){
  17. System.out.println("mehthod run");
  18. }
  19. };
  20. }
  21. }
  22. public class Demo{
  23. public static void main(String[] args){
  24. Test.function().method();
  25. }
  26. }


五、异常
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、例子
[java] view plaincopy
  1. class ExceptionDemo{
  2. int div(int a,int b){
  3. return a/b;
  4. }
  5. }
  6. public class Demo{
  7. public static void main(String[] args){
  8. ExceptionDemo ed = new ExceptionDemo();
  9. int x = ed.div(4,0);
  10. //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)
  11. System.out.println("x="+x);
  12. System.out.println("over");
  13. }
  14. }


六、异常try-catch
1、异常的处理
try{
需要被检测的代码
}catch(){
处理异常的代码:(处理方式)
}finally{
一定会执行的语句
}
2、例子
[java] view plaincopy
  1. class ExceptionDemo{
  2. int div(int a,int b){
  3. return a/b; //产生了 ArithmeticException,并把问题封装成了一个对象
  4. }
  5. }
  6. public class Demo{
  7. public static void main(String[] args){
  8. ExceptionDemo ed = new ExceptionDemo();
  9. try{
  10. int x = ed.div(4,0); //new ArithmeticException()
  11. System.out.println("x="+x); //因为上边已经发生了异常,所以这一步不会再执行
  12. }catch(Exception e){ //Exception e = ArithmeticException();
  13. System.out.println("除零了");
  14. System.out.println(e.toString());//异常名称:异常信息
  15. System.out.println(e.getMessage());//by zero
  16. System.out.println(e.printStackTrace());//异常名称,异常信息,异常出现的位置
  17. //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。打印异常的堆栈的跟踪信息。
  18. }
  19. System.out.println("over");
  20. }
  21. }


3、对捕获到的异常进行常见方法操作。
(1)、String getMessage():获取异常信息
(2)、toString():返回此 throwable 的简短描述
七、异常声明throws
1、在类的函数上 声明 throws Exception,在对象在调用函数时,必须捕捉或者声明抛出
2、声明异常时,建议声明更具体的异常,这样处理可以更具体
3、对方声明几个异常,就对应有几个catch块
(1)、如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面
4、建立在进行catch处理是,catch中一定要定义具体处理方式,不要简单定义一句,e.printStackTrace(),也不要简单的就书写出一条语句
5、
[java] view plaincopy
  1. class ExceptionDemo{
  2. int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{ //再功能上通过 throws 的关键字声明了该功能有可能会出现问题,可以声明多个异常
  3. int[] arr = new int[a];
  4. System.out.println(arr[3]);//虽然有多个异常,但是当前面有异常时,该方法块内,异常后面的就不会运行
  5. return a/b;
  6. }
  7. }
  8. public class Demo{
  9. public static void main(String[] args){
  10. ExceptionDemo ed = new ExceptionDemo();
  11. try{
  12. int x = ed.div(3,0);
  13. System.out.println("x="+x);
  14. }catch(ArithmeticException e){
  15. System.out.println("除零了");
  16. }catch(ArrayIndexOutOfBoundsException e){
  17. System.out.println("角标越界了");
  18. }
  19. System.out.println("over");
  20. }
  21. }


八、自定义异常
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,也视为是错误
[java] view plaincopy
  1. */
  2. class FuShuException extends Exception{
  3. private int value;
  4. FuShuException(String msg,int value){
  5. super(msg);//定义异常信息,因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,那么就可以直接通过 getMessage 方法获取自定义的异常信息
  6. this.value = value;
  7. }
  8. public int getValue(){
  9. return value;
  10. }
  11. }
  12. class ExceptionDemo{
  13. int div(int a,int b) throws FuShuException{ //函数内出现异常,函数上需要声明
  14. if(b<0){
  15. throw new FuShuException("除数为负数了",b);
  16. }
  17. return a/b;
  18. }
  19. }
  20. public class Demo{
  21. public static void main(String[] args){
  22. ExceptionDemo ed = new ExceptionDemo();
  23. int x = 0;
  24. try{
  25. x = ed.div(3,-1);
  26. }catch(FuShuException e){
  27. System.out.println(e.toString());
  28. System.out.println("该负数是:"+e.getValue());
  29. }
  30. System.out.println("x="+x);
  31. System.out.println("结束");
  32. }
  33. }


十、throw和throws的区别
1、throws 使用在函数上,throw 使用在函数内
2、throws 后面跟的异常类,可以跟多个,用逗号隔开,throw 后面跟的是异常对象。
十一、RuntimeException
1、Exception中有一个特殊的子类异常, RuntimeException 运行异常
2、如果在函数内抛出异常,函数上可以不用声明,编译一样通过
3、如果在函数上声明了该异常,调用者可以不用进行处理。编译一样通过
4、之所以不用在函数上声明,是因为不需要让调用者处理。当该异常发生,希望程序停止,因为在运行时,出现了无法继续的运算情况,希望停止程序后,对代码进行修正。
5、自定义异常时,如果该异常的发生,无法再继续进行运算,就让自定义异常继承 RuntimeException
6、对于异常,分两种
(1)、编译时被检测的异常(如Exception)
(2)、编译时不被检测的异常(运行时异常,RuntimeException以及子类)

十二、练习

[java] view plaincopy
  1. /*
  2. 毕老师用电脑上课
  3. 上课中出现的问题
  4. 电脑蓝屏。电脑冒烟
  5. 对问题进行描述,封装成对象
  6. 可是当冒烟发生后,出现讲课进度无法继续。
  7. 出现了讲师的问题,课时计划无法完成。
  8. */
  9. class LanPingException extends Exception{
  10. LanPingException(String message){
  11. super(message);
  12. }
  13. }
  14. class MaoYanException extends Exception{
  15. MaoYanException(String message){
  16. super(message);
  17. }
  18. }
  19. class noPlanException extends Exception{
  20. noPlanException(String message){
  21. super(message);
  22. }
  23. }
  24. class Computer{
  25. private int state = 3;//1为正常状态,
  26. // Computer(){
  27. // }
  28. public void run() throws LanPingException,MaoYanException{
  29. if(state==2){
  30. throw new LanPingException("蓝屏了");
  31. }else if(state==3){
  32. throw new MaoYanException("冒烟了");
  33. }
  34. System.out.println("电脑运行");
  35. }
  36. public void reset(){
  37. state = 1;
  38. System.out.println("电脑重启");
  39. }
  40. }
  41. class Teacher{
  42. private String name;
  43. private Computer cmpt;
  44. Teacher(String name){
  45. this.name = name;
  46. cmpt = new Computer();
  47. }
  48. public void prelect() throws noPlanException{
  49. try{
  50. cmpt.run();
  51. }catch(LanPingException e){
  52. cmpt.reset();
  53. }catch(MaoYanException e){
  54. test();
  55. throw new noPlanException("课时无法继续,"+e.getMessage());
  56. }
  57. System.out.println(name+"老师上课");
  58. }
  59. public void test(){
  60. System.out.println("做练习");
  61. }
  62. }
  63. class Demo{
  64. public static void main(String[] args){
  65. Teacher t = new Teacher("毕老师");
  66. try{
  67. t.prelect();
  68. }catch(noPlanException e){
  69. System.out.println(e.getMessage());
  70. System.out.println("换老师或者换电脑或者放假");
  71. }
  72. }
  73. }

读书人网 >编程

热点推荐