zoukankan      html  css  js  c++  java
  • 黑马程序员面向对象09天3

    /*
    毕老师用电脑上课。
    
    开始思考上课中出现的问题。
    
    
    比如问题是
        电脑蓝屏。
        电脑冒烟。
    
    要对问题进行描述,封装成对象。
    
    
    可是当冒烟发生后,出现讲课进度无法继续。
    
    出现了讲师的问题:课时计划无法完成。
    
    
    */
    
    class LanPingException extends Exception
    {
        LanPingException(String message)
        {
            super(message);
        }
    }
    
    class MaoYanException extends Exception
    {
        MaoYanException(String message)
        {
            super(message);
        }
    }
    
    
    class NoPlanException extends Exception
    {
        NoPlanException(String msg)
        {
            super(msg);
        }
    }
    
    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 reset()
        {
            state = 1;
            System.out.println("电脑重启");
            
        }
    }
    
    class Teacher
    {
        private String name;
        private Computer cmpt;
    
        Teacher(String name)
        {
            this.name = name;
            cmpt = new Computer();
    
        }
    
        public void prelect()throws NoPlanException
        {
            try
            {
                cmpt.run();            
            }
            catch (LanPingException e)
            {
                cmpt.reset();
            }
            catch (MaoYanException e)
            {
                
                test();
                throw new NoPlanException("课时无法继续"+e.getMessage());
                
            }
            System.out.println("讲课");
        }
        public void test()
        {
            System.out.println("练习");
        }
    
    }
    
    
    
    class ExceptionTest 
    {
        public static void main(String[] args) 
        {
            Teacher t = new Teacher("毕老师");
            try
            {
                t.prelect();
            }
            catch (NoPlanException e)
            {
                System.out.println(e.toString());
                System.out.println("换老师或者放假");
            }
            
        }
    }
  • 相关阅读:
    oracle闪回某个时间段的数据
    查询某个表某个字段重复记录急重复数量
    调用腾讯QQ启动
    MongoDB笔记(二):MongoDB下Shell的基本操作
    MongoDB笔记(一):MongoDB介绍及Windows下安装
    freemarker相关
    oracle获取时间毫秒数
    如何简单地理解Python中的if __name__ == '__main__'
    python套接字基本使用
    Mysql表的约束设计和关联关系设计
  • 原文地址:https://www.cnblogs.com/guwenren/p/3006365.html
Copyright © 2011-2022 走看看