您的位置:首页 > 其它

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: