从线程之前我进行巩固,因为初学担心之后会跟不上。继续分享异常的电脑冒烟问题。挺乱的,这个代码只敲了一遍。之后会把之前到异常的所有代码自己回忆敲一敲。请忽略其中的函数名字。
/*
毕老师用电脑讲课
分析其中的问题,电脑蓝屏
电脑冒烟
当电脑冒烟发生后,出现讲课无法进行
出现了讲师问题,课时无法完成。
*/
//无法讲课异常
class NoException extends Exception
{
NoException(String ms)
{
super(ms);
}
}
//电脑蓝屏了
class LanPingException extends Exception
{
LanPingException(String ms)
{
super(ms);
}
}
//电脑冒烟了
class MaoYanException extends Exception
{
MaoYanException(String ms)
{
super(ms);
}
}
//用电脑讲课,有一台电脑
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 rester()
{
state = 1;
System.out.println("电脑重启");
}
}
//首先毕老师讲课
class Teacher
{
private String name;//有名字
private Computer cmpt;//讲课有电脑
Teacher(String name)
{
this.name = name;
cmpt = new Computer();
}
//有讲课的方法
public void Jiangke()throws NoException
{
try
{
cmpt.run();
}
catch (LanPingException e)
{
cmpt.rester();
}
catch(MaoYanException e)
{
test();
throw new NoException("无法讲课了"+e.getMessage());
}
System.out.println("讲课");
}
public void test()
{
System.out.println("自习");
}
}
class ExceptionTest2
{
public static void main(String[] args)
{
Teacher t = new Teacher("毕老师");
try
{
t.Jiangke();
}
catch (NoException e)
{
System.out.println(e.toString() );
System.out.println("换老师或者放假");
}
}
}
|
|