• 多态与异常处理动手动脑


    1、编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    import java.util.Scanner;
    
    public class CourseEvaluation {
    
        public static void main(String[] args) throws Exception {//抛出异常信息
            int n = 0;
            while(n!=1)
            {
            System.out.println("请输入成绩:");
            Scanner c = new Scanner(System.in);
            String str = c.nextLine();//对出入的数据进行读取
            
            try
            {
                double score = Double.parseDouble(str); //把输入的值转化成INT类型
                if(score >= 0 && score < 60){
                    System.out.println("不及格");
                    n=1;
                }
                
                else if(score >= 60 && score < 70){
                    System.out.println("及格");
                    n=1;
                }
                
                else if(score >= 70 && score < 80){
                    System.out.println("中");
                    n=1;
                }
                
                else if(score >= 80 && score < 90){
                    System.out.println("良");
                    n=1;
                }
                
                else if(score >= 90 && score <= 100){
                    System.out.println("优");
                    n=1;
                }
                
                else{
                    System.out.println("成绩无效!");
                }
            }
            
            catch(NumberFormatException e)
            {
                System.out.println("成绩输入错误,^_^!!");
            }//捕捉错误
          
        }
        }
    }

    2、示例ParentChildTest.java

    代码:

    public class ParentChildTest {
        public static void main(String[] args) {
            Parent parent=new Parent();
            parent.printValue();
            Child child=new Child();
            child.printValue();
            
            parent=child;
            parent.printValue();
            
            parent.myValue++;
            parent.printValue();
            
            ((Child)parent).myValue++;
            parent.printValue();
            
        }
    }
    
    class Parent{
        public int myValue=100;
        public void printValue() {
            System.out.println("Parent.printValue(),myValue="+myValue);
        }
    }
    class Child extends Parent{
        public int myValue=200;
        public void printValue() {
            System.out.println("Child.printValue(),myValue="+myValue);
        }
    }

    验证截图:

    总结:当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。这个特性实际上就是面向对象“多态”特性的具体表现。

    如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。

    如果子类被当作父类使用,则通过子类访问的字段是父类的!

    3、请阅读并运行AboutException.java示例

    截图:

    4、阅读以下代码(CatchWho.java),写出程序运行结果:

    验证截图:

    5、写出CatchWho2.java程序运行的结果

    验证截图:

    6、请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

    代码:

    public class EmbededFinally {
    
        
        public static void main(String args[]) {
            
            int result;
            
            try {
                
                System.out.println("in Level 1");
    
               
                 try {
                    
                    System.out.println("in Level 2");
      // result=100/0;  //Level 2
                   
                     try {
                       
                         System.out.println("in Level 3");
                          
                         result=100/0;  //Level 3
                    
                    } 
                    
                    catch (Exception e) {
                        
                        System.out.println("Level 3:" + e.getClass().toString());
                    
                    }
                    
                    
                    finally {
                        
                        System.out.println("In Level 3 finally");
                    
                    }
                    
                   
                    // result=100/0;  //Level 2
    
                
                    }
                
                catch (Exception e) {
                   
                     System.out.println("Level 2:" + e.getClass().toString());
               
                 }
                 finally {
                    
                    System.out.println("In Level 2 finally");
               
                 }
                 
                // result = 100 / 0;  //level 1
            
            } 
            
            catch (Exception e) {
                
                System.out.println("Level 1:" + e.getClass().toString());
            
            }
            
            finally {
               
    .             System.out.println("In Level 1 finally");
            
            }
        
        }
    
    }

    结果截图:

    总结:

    当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

    7、请通过 SystemExitAndFinally.java示例程序回答finally语句块一定会执行吗?

    验证截图:

    总结:

    不一定会执行,如果一个方法内在执行try{}语句之前就已经return了,那么finally语句指定不会执行了。因为它根本没有进入try语句中, 如果在一个try语句中调用System.exit(0);方法,那么就会退出当前java虚拟机,那么finally也就没有执行的机会了。

    8、通过 PrintExpressionStack.java示例总结:

    当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。

    可使用printStackTrace 和 getMessage方法了解异常发生的情况:

    printStackTrace:打印方法调用堆栈。

    每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

  • 相关阅读:
    有趣的机器学习视频-第四章.强化学习
    有趣的机器学习视频-第五章.进化算法
    有趣的机器学习视频-第二章.人工神经网络
    有趣的机器学习视频-第三章.如何评价/检验神经网络
    有趣的机器学习视频-第一章.什么是机器学习
    AlphaStar
    AlphaZero
    AlphaGo分析
    nfs自动挂载
    postfix简单记录
  • 原文地址:https://www.cnblogs.com/lk0823/p/4965158.html
Copyright © 2020-2023  润新知