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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. class LanPingException extends Exception
  2. {
  3.         LanPingException(String msg)
  4.         {
  5.                 super(msg);
  6.         }
  7. }

  8. class MaoYanException extends Exception
  9. {
  10.         MaoYanException(String msg)
  11.         {
  12.                 super(msg);
  13.         }
  14. }

  15. class NoPlanException extends Exception
  16. {
  17.         NoPlanException(String msg)
  18.         {
  19.                 super(msg);
  20.         }
  21. }

  22. class Computer
  23. {
  24.         private int state=2;
  25.         public void run() throws LanPingException,MaoYanException
  26.         {
  27.                 if (state==2)
  28.                         throw new LanPingException("电脑出现蓝屏");
  29.                 if (state==3)
  30.                         throw new MaoYanException("电脑冒烟了");
  31.                 System.out.println("电脑正常运行");
  32.         }
  33.         public void reset()
  34.         {
  35.                 state=1;
  36.                 System.out.println("电脑重启");
  37.                 //run();想在这个地方继续调用一次run()方法,打印"电脑正常运行",不知道怎样调用run()方法!!!求解。。。。
  38.         }
  39. }

  40. class Teacher
  41. {
  42.         private String name;
  43.         private Computer cmpt;
  44.         Teacher(String name)
  45.         {
  46.                 this.name=name;
  47.                 cmpt=new Computer();
  48.         }
  49.         public void teach() throws NoPlanException
  50.         {
  51.                 try
  52.                 {
  53.                         cmpt.run();
  54.                 }
  55.                 catch (LanPingException e)
  56.                 {
  57.                         cmpt.reset();
  58.                 }
  59.                 catch (MaoYanException e)
  60.                 {
  61.                         test();
  62.                         throw new NoPlanException("课时无法继续,"+e.getMessage());
  63.                 }
  64.                
  65.                 System.out.println("老师上课");
  66.         }
  67.         public void test()
  68.         {
  69.                 System.out.println("做练习......");
  70.         }

  71. }

  72. class ExceptionDemo
  73. {
  74.         public static void main(String[] args)
  75.         {
  76.                 Teacher t=new Teacher("毕老师");
  77.                 try
  78.                 {
  79.                         t.teach();
  80.                 }
  81.                 catch (NoPlanException e)
  82.                 {
  83.                         System.out.println(e.toString());
  84.                         System.out.println("换老师或者放假");
  85.                 }
  86.                
  87.         }
  88. }
复制代码

当时毕老师在视频中也出错了,但是没有调试,自试了很多方法,但是也没成功,求大神指点。。。

2 个回复

倒序浏览
请各位大神帮我看看第40行吧,,谢谢了:(
回复 使用道具 举报
this.run();
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马