A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kj1989 中级黑马   /  2013-12-28 15:11  /  1067 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 kj1989 于 2013-12-29 15:26 编辑

为什么在一个方法被覆盖时,覆盖它的方法必须扔出相同的异常或异常的子类呢?哪位高手能给个解释?

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 淡定

查看全部评分

2 个回复

倒序浏览
本帖最后由 胡永城 于 2013-12-28 16:42 编辑

1、覆盖的方法的标志符必须要和被覆盖的方法的标志符完全匹配,才能达到覆盖的效果;
2、覆盖的方法的返回值必须和被覆盖的方法的返回一致;
3、覆盖的方法所抛出的异常必须和被覆盖方法的所抛出的异常一致,或者是其子类;
4、被覆盖的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行覆盖。


覆盖呀,只有方法声明完全一致才可以称之为覆盖。抛出异常也是声明的一部分

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

回复 使用道具 举报
原因:早期程序只能处理早期异常,不能处理后期产生的异常,以下以程序说明
1,父类抛出FuException,子类也抛出FuException
当otherMethod(Fu f)调用exceptonMethod()时根据多态原理,运行子类的exceptonMethod方法,并抛出FuException异常,此时方法的catch为catch (FuException  e)因此可以处理这个异常,打印“出错了”,

  1. class FuException extends Exception
  2. {
  3.         FuException(String message)
  4.         {
  5.                 super(message);
  6.         }
  7. }
  8. class ZiException extends Exception
  9. {
  10.         ZiException(String message)
  11.         {
  12.                 super(message);
  13.         }
  14. }
  15. class Fu
  16. {
  17.         public void exceptonMethod()throws FuException
  18.         {
  19.                 throw new FuException("父类异常");
  20.         }

  21. }
  22. class Zi extends Fu
  23. {
  24.         public void exceptonMethod()throws FuException
  25.         {
  26.                 throw new FuException("子类异常");
  27.         }
  28. }

  29. class Other
  30. {
  31.         public void otherMethod(Fu f)
  32.         {
  33.                 try
  34.                 {
  35.                         f.exceptonMethod();
  36.                 }
  37.                 catch (FuException e)
  38.                 {
  39.                         System.out.println("出错了");
  40.                 }
  41.         }
  42. }
  43. class MyTest
  44. {
  45.         public static void main(String[] args)
  46.         {
  47.                 Zi z = new Zi();
  48.                 Other o = new Other();
  49.                 o.otherMethod(z);
  50.                
  51.         }
  52. }
复制代码


2,父类抛出FuException,子类抛出ZiException
当otherMethod(Fu f)调用exceptonMethod()时根据多态原理,运行子类的exceptonMethod方法,并抛出ZiException异常,此时方法的catch为catch (FuException  e)因此不可以处理这个异常导致程序无法进行
  1. class FuException extends Exception
  2. {
  3.         FuException(String message)
  4.         {
  5.                 super(message);
  6.         }
  7. }
  8. class ZiException extends Exception
  9. {
  10.         ZiException(String message)
  11.         {
  12.                 super(message);
  13.         }
  14. }
  15. class Fu
  16. {
  17.         public void exceptonMethod()throws FuException
  18.         {
  19.                 throw new FuException("父类异常");
  20.         }

  21. }
  22. class Zi extends Fu
  23. {
  24.         public void exceptonMethod()throws ZiException
  25.         {
  26.                 throw new ZiException("子类异常");
  27.         }
  28. }

  29. class Other
  30. {
  31.         public void otherMethod(Fu f)
  32.         {
  33.                 try
  34.                 {
  35.                         f.exceptonMethod();
  36.                 }
  37.                 catch (FuException e)
  38.                 {
  39.                         System.out.println("出错了");
  40.                 }
  41.         }
  42. }
  43. class MyTest
  44. {
  45.         public static void main(String[] args)
  46.         {
  47.                 Zi z = new Zi();
  48.                 Other o = new Other();
  49.                 o.otherMethod(z);
  50.                
  51.         }
  52. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 好长~~~~~~~~~~~~~~~~

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马