对象(YiChanTest)

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!!!");
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值