java 面向对象编程(模拟老师用电脑上课)

//程序入口类

/*
 * 陈老师用电脑上课。
 * 课上出现的问题
 * 比如: 
 * 电脑蓝屏了。
 * 电脑冒烟了。
 * 对问题进行描述,封装成对象。
 *  当冒烟MaoyanException发生后,课时无法继续。
 *  出现了讲师的问题,课时计划无法继续NoplanException
 */
public class ExceptionTest1 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher t=new Teacher("陈老师");
try {
t.prelect();
} catch (NoplanException e) {
// TODO: handle exception
System.out.println(e.toString());
System.out.println("放假!");
}


}


}


//讲师类
public class Teacher {
private String name;
private Computer cmpt;

public Teacher(String name) {
// TODO Auto-generated constructor stub
this.name=name;
cmpt=new Computer();

}
//public void prelect()throws MaoyanException{
public void prelect()throws NoplanException{
try {
cmpt.run();
} catch (LanpingException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
cmpt.reset(); 
} catch (MaoyanException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
test();
throw new NoplanException("课时无法继续"+e.getMessage());
}
System.out.println("讲课"); 
}
public void test(){
System.out.println("练习");
}

}

//电脑类



public class Computer {
//private int state=1;
private int state=2;
//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("电脑重启");
}

}

//蓝屏类
public class LanpingException extends Exception {
LanpingException(String message){
super(message);
}
}

//冒烟类

public class MaoyanException extends Exception {
 MaoyanException(String message){
super(message);
 }
 
}

//课时计划无法继续类

public class NoplanException extends Exception {
  NoplanException(String message){
 super(message);
  }
}

你可能感兴趣的:(java 面向对象编程(模拟老师用电脑上课))