-------
android培训、
java培训、期待与您交流! ----------
需求,老师用电脑上课,可以会出现问题
1.电脑在使用过程中死机,蓝屏 ,电脑重起就可以
2.电脑主板烧坏,冒烟 电脑需要维修.
我们在开发中前其需要对可能出现的问题进行分析,
将这些问题声明成自定义异常
老师上课使用电脑 有两个实体 1.Teacher类 2.Computer类
class Test1
{
public static void main(String[] args)
{
Teacher t=new Teacher();
try{
t.teach();
}catch(ChangeZYException e){
System.out.println(e.getMessage());
System.out.println("教学部主管理处理");
}
}
}
//老师
class Teacher
{
Computer c=new Computer();
//授课
void teach() {
//打开电脑,电脑运行
try{
c.run();
//开始上课
System.out.println("开始上课");
}catch(LanPingException e){
//如果执行到这个位置,说明老师发现电脑蓝屏,
System.out.println(e.getMessage());
c.reset();
this.teach();
}catch(MaoYanException e){
//电脑需要维修.
System.out.println(e.getMessage());
System.out.println("让学生做练习");
//new ZGC().xl(c);
//为什么将异常进行转型,你应该给调用你的人抛出一个他可以处理的问题
throw new ChangeZYException(e.getMessage()+" 看着处理吧...");
//teach();
}
}
}
//我们应该定义一个维修的行为
class ZGC{
void xl(Computer c){
c.state=0;
System.out.println("电脑进行了维修");
}
}
//电脑
class Computer
{
//定义一个变量来描述电脑的状态 0 正常 1 蓝屏 2 冒烟
int state=2;
//电脑运行 ,因为两个异常都是编译异常
void run() throws LanPingException,MaoYanException{
if(state==0){ //正常状态
System.out.println("电脑运行");
}else if(state==1){//蓝屏
throw new LanPingException("电脑蓝屏...");
}else if(state==2){//冒烟
throw new MaoYanException("冒烟....");
}
}
//重启
void reset(){
System.out.println("电脑重新启动");
state=0;
}
}
/*自定义异常*/
//蓝屏
class LanPingException extends Exception
{
public LanPingException(String message){
super(message);
}
}
//冒烟
class MaoYanException extends Exception
{
public MaoYanException(String message){
super(message);
}
}
//改变作业
class ChangeZYException extends RuntimeException
{
public ChangeZYException(String message){
super(message);
}
}
需求,老师用电脑上课,可以会出现问题
1.电脑在使用过程中死机,蓝屏 ,电脑重起就可以
2.电脑主板烧坏,冒烟 电脑需要维修.
我们在开发中前其需要对可能出现的问题进行分析,
将这些问题声明成自定义异常
老师上课使用电脑 有两个实体 1.Teacher类 2.Computer类
class Test1
{
public static void main(String[] args)
{
Teacher t=new Teacher();
try{
t.teach();
}catch(ChangeZYException e){
System.out.println(e.getMessage());
System.out.println("教学部主管理处理");
}
}
}
//老师
class Teacher
{
Computer c=new Computer();
//授课
void teach() {
//打开电脑,电脑运行
try{
c.run();
//开始上课
System.out.println("开始上课");
}catch(LanPingException e){
//如果执行到这个位置,说明老师发现电脑蓝屏,
System.out.println(e.getMessage());
c.reset();
this.teach();
}catch(MaoYanException e){
//电脑需要维修.
System.out.println(e.getMessage());
System.out.println("让学生做练习");
//new ZGC().xl(c);
//为什么将异常进行转型,你应该给调用你的人抛出一个他可以处理的问题
throw new ChangeZYException(e.getMessage()+" 看着处理吧...");
//teach();
}
}
}
//我们应该定义一个维修的行为
class ZGC{
void xl(Computer c){
c.state=0;
System.out.println("电脑进行了维修");
}
}
//电脑
class Computer
{
//定义一个变量来描述电脑的状态 0 正常 1 蓝屏 2 冒烟
int state=2;
//电脑运行 ,因为两个异常都是编译异常
void run() throws LanPingException,MaoYanException{
if(state==0){ //正常状态
System.out.println("电脑运行");
}else if(state==1){//蓝屏
throw new LanPingException("电脑蓝屏...");
}else if(state==2){//冒烟
throw new MaoYanException("冒烟....");
}
}
//重启
void reset(){
System.out.println("电脑重新启动");
state=0;
}
}
/*自定义异常*/
//蓝屏
class LanPingException extends Exception
{
public LanPingException(String message){
super(message);
}
}
//冒烟
class MaoYanException extends Exception
{
public MaoYanException(String message){
super(message);
}
}
//改变作业
class ChangeZYException extends RuntimeException
{
public ChangeZYException(String message){
super(message);
}
}