zoukankan      html  css  js  c++  java
  • 多态与异常处理课后习题

    1.

    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);

             }

    }

    实验截图:

     

    总结:在父类与子类有相同方法时,如果是子类对象就调用子类方法,父类对象调用父类方法。

    2.CathWho

    public class CatchWho {

        public static void main(String[] args) {

            try {

                try {

                    throw new ArrayIndexOutOfBoundsException();

                }

                catch(ArrayIndexOutOfBoundsException e) {

                   System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

                }

                throw new ArithmeticException();

            }

            catch(ArithmeticException e) {

                System.out.println("发生ArithmeticException");

            }

            catch(ArrayIndexOutOfBoundsException e) {

               System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

            }

        }

    }

    截图·:

     

    3. CathWho2

    public class CatchWho2 {

        public static void main(String[] args) {

            try {

                try {

      

                throw new ArithmeticException();

            }

            catch(ArithmeticException e) {

                System.out.println("发生ArithmeticException");

            }

                    throw new ArrayIndexOutOfBoundsException();

                }

                catch(ArithmeticException e) {

                    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

                }

          

          

            catch(ArrayIndexOutOfBoundsException e) {

                System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

            }

        }

    }

    截图

     

    4 .EmbededFinally

    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");

           

                       }

       

             }

    }

    实验截图:

     

    总结:丢一个包,然后利用catch去捕捉那个包。不同类名的包必须用与包同类型的catch去捕捉。

    5. SystemExitAndFinally.java

    public class SystemExitAndFinally {

        public static void main(String[] args)

        {

            try{

                System.out.println("in main");

                throw new Exception("Exception is thrown in main");

                //System.exit(0);

            }

            catch(Exception e)

            {

                System.out.println(e.getMessage());

                System.exit(0);

            }

            finally

            {

                System.out.println("in finally");

            }

        }

    }

    实验截图:

     

    总结:finally语句一定会执行。

    6. PrintExpressionStack.java

    // UsingExceptions.java

    // Demonstrating the getMessage and printStackTrace

    // methods inherited into all exception classes.

    public class PrintExceptionStack {

       public static void main( String args[] )

       {

          try {

             method1();

          }

          catch ( Exception e ) {

             System.err.println( e.getMessage() + " " );

             e.printStackTrace();

          }

       }

       public static void method1() throws Exception

       {

          method2();

       }

       public static void method2() throws Exception

       {

          method3();

       }

       public static void method3() throws Exception

       {

          throw new Exception( "Exception thrown in method3" );

       }

    }

    截图:

     

  • 相关阅读:
    mp3播放器(四)(从服务器端下载mp3文件+service的应用+SDcard操作)
    mp3播放器(二)(adapter模式将显示在屏幕上)
    HTML日记一(表格,图片,超链接,无序列表,frameset框架集)
    HTML日记三(地图图片映射)
    聚集索引和非聚集索引[转]
    linq 解决winForm中控件CheckedListBox操作的问题。(转载)
    C#抓取网页数据、分析并且去除HTML标签(转载)
    Cachecontrol使用:header('Cachecontrol:private')
    缓存DataSet(转载)
    jquery调用基于.NET Framework 3.5的WebService返回JSON数据 (转)
  • 原文地址:https://www.cnblogs.com/zhng921/p/4955709.html
Copyright © 2011-2022 走看看