/*
基于面向对象的思想对“老师上课发生的事进行描述”
老师上课
属性:姓名
功能:讲课
使用电脑讲课
功能:运行,就上课
重启,接着上课
可能出现的问题:对问题封装
电脑蓝屏 (重启)
电脑冒烟(换人或放假)
*/
//蓝屏对象
class LanPingException extends Exception
{
LanPingException(String message)
{
super(message);
}
}
//冒烟对象
class MaoYanException extends Exception
{
MaoYanException(String message)
{
super(message);
}
}
//将冒烟的问题转化成可以处理的问题
class NoPlanException extends Exception
{
NoPlanException(String message)
{
super(message);
}
}
//对象为:电脑
//具备的功能:运行,重启
class Computer
{
private int state=3;//电脑状态
public void run() throws LanPingException,MaoYanException
{
if(state==2)
throw new LanPingException("蓝屏了");
if(state==3)
throw new MaoYanException("冒烟了");
System.out.println("电脑运行");
}
public void reset()
{
state=1;
System.out.println("电脑重启");
}
}
//对象为:老师
//功能为:讲课
//属性:姓名
class Teacher
{
private String name;
private Computer comp;
Teacher(String name)
{
comp = new Computer();
this.name = name;
}
public void jiangKe() throws NoPlanException
{
try
{
comp.run();
}
catch(LanPingException e)
{
comp.reset();
}
catch(MaoYanException e)
{
Test();
throw new NoPlanException("课时无法继续"+e.getMessage());
//Test(); 无法访问到,因为异常抛出,程序结束
}
System.out.println(name+",老师上课");
}
public void Test()
{
System.out.println("做练习");
}
}
class ExceptionTest
{
public static void main(String [] args)
{
Teacher t = new Teacher("比向东");
try
{
t.jiangKe();
}
catch(NoPlanException e)
{
System.out.println(e.toString());
System.out.println("换老师或者放假");
}
}
}
|