对象(YiChanTest)
2016-03-05 13:02
211 查看
package duixiang; class Teacher { private String name; Computer c;//建立全局变量,方便后面的teaching方法调用 Teacher(String name) { this.name=name; c=new Computer(); } void teaching(int n)throws TeacherTrableException { try{ c.run(n); }catch(ComputerTrable1Exception e) { System.out.println(e.toString()); c.restart(); c.n=0; }catch(ComputerTrable2Exception e) { test(); throw new TeacherTrableException(e.getMessage()+" "+name+" Cannot teaching!!!");//此处应该转化为调用函数能理解的异常 } System.out.println(name+" Has a Teaching!!!"); } void test() { System.out.println("Test!!!"); } } class Computer { int n; public void run(int n)throws ComputerTrable1Exception,ComputerTrable2Exception { this.n=n; if(n==0) System.out.println("Computer start"); else if(n==1) throw new ComputerTrable1Exception("Comeputer need Restart!!!"); else if(n==2) throw new ComputerTrable2Exception("Computer cannot work!!!"); } public void restart() { System.out.println("Computer restart"); } } class ComputerTrable1Exception extends Exception { ComputerTrable1Exception(String s){ super(s); } } class ComputerTrable2Exception extends Exception { ComputerTrable2Exception(String s) { super(s); } } class TeacherTrableException extends Exception { TeacherTrableException(String s) { super(s); } } public class YiChanTest { public static void main(String [] args) { Teacher t=new Teacher("Sir Bi"); try{ t.teaching(0); }catch(TeacherTrableException e) { System.out.println(e.toString()); System.out.println("Stop teaching!!!"); } } }
相关文章推荐
- 拉格朗日乘数法
- JavaScript学习笔记——语句
- C++项目文件中的后缀名为SDF的文件是什么?有什么用?
- cin和scanf的效率差异
- day09—oracle视图、序列、索引、约束
- JS面向对象组件(一) ---包装对象与原型链
- New Beginning --- PAT_Basic 1001
- 对象(YiChanDemo)
- 分享Android及IOS开源实例代码网址
- Java API 与HBase交互实例
- intellij idea 安装mahout_in_action项目
- Java之集合类应用总结
- CSS3中动画效果Transitions与Animations的区别
- 数据结构和算法
- 常用环境变量配置
- HashMap的两种排序方式
- 最大M子段和
- GDKOI2016 Day2 T3 项链
- ECMAScript arguments 对象
- 对象(Single)