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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kemeng 中级黑马   /  2015-3-13 16:46  /  465 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 毕老师用电脑上课
  3. 电脑出现问题:
  4. 蓝屏
  5. 冒烟
  6. */
  7. class LanpingException extends Exception
  8. {
  9.         LanpingException(String s)
  10.         {
  11.                 super(s);
  12.         }
  13. }
  14. class MaoyanException extends Exception
  15. {
  16.         MaoyanException(String message)
  17.         {
  18.                 super(message);
  19.         }
  20.        
  21. }
  22. class NoplanException extends Exception
  23. {
  24.         NoplanException(String message)
  25.         {
  26.                 super(message);
  27.         }
  28. }

  29. class Computer
  30. {
  31.         private int state=3;
  32.         public void run() throws LanpingException,MaoyanException
  33.         {
  34.                 if(state==2)
  35.                         throw new LanpingException("电脑蓝屏了");
  36.                 if(state==3)
  37.                         throw new MaoyanException("电脑冒烟了");

  38.                 System.out.println("电脑运行");
  39.         }
  40.         public void reset()
  41.         {
  42.                 System.out.println("电脑重启");
  43.         }
  44. }
  45. class Teacher
  46. {
  47.         private String name;
  48.         private Computer cmpt;
  49.         Teacher(String name)
  50.         {
  51.                 this.name=name;
  52.                 cmpt=new Computer();
  53.         }
  54.         public void prelect() throws NoplanException
  55.         {
  56.                 try
  57.                 {
  58.                         cmpt.run();
  59.                 }
  60.                 catch (LanpingException e)
  61.                 {
  62.                         cmpt.reset();
  63.                 }
  64.                 catch(MaoyanException e)
  65.                 {
  66.                         test();
  67.                         throw new NoplanException("课程无法继续"+e.getMessage());
  68.                 }
  69.                
  70.                 System.out.println("讲课");
  71.         }
  72.         public void test()
  73.         {
  74.                 System.out.println("做练习");
  75.         }
  76. }


  77. class Demo6
  78. {
  79.         public static void main(String[] args)
  80.         {
  81.                 Teacher t=new Teacher("毕老师");
  82.                 try
  83.                 {
  84.                         t.prelect();
  85.                
  86.                 }
  87.                 catch (NoplanException e)
  88.                 {
  89.                         System.out.println(e.toString());
  90.                         System.out.println("换老师或者放假");
  91.                 }
  92.                

  93.         }
  94. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马