动手动脑

一、运行课件中的例题ParentChildTest.java,回答下列问题:

a、左边的结果运行结果是什么?

运行结果截图为:

b、你如何解释会得到这样的输出?

第一个值:父类Parent调用自己的方法;

第二个值:子类child调用方自己的方法;

第三个值:子类将自己的方法赋给父类,因而实际调用的是子类的方法;

第四个值:父类中的值MyValue加一,但是父类调用的是子类的方法,因而无影响;

第五个值:强制转化,父类将值赋给子类,调用的仍然是子类的方法,只是值加一

c、计算机是不会出错的,之所以会得到这样的运行结果也是有原因的,那么从运行结果当中,你能总结出java的那些语法特征?

① 用类名进行赋值时(如parent=child ),附的都是方法;

② 对类中的值进行赋值时需要用类名.对象;

③ 父类对子类进行赋值时需要强制转化;

(1)、并修改ParentChildTest.java,验证你的回答结果。

  验证程序代码如下:

public class ParentChileTest {

public static void main(String[] args) {

Parent parent=new Parent();

parent.printValue();

Child child=new Child();

child.printValue();

parent=child;

parent.printValue();

parent.myValue ++;

System.out.println(parent.myValue);

System.out.println(child.myValue);

parent.printValue();

((Child)parent).myValue++;

System.out.println(parent.myValue);

System.out.println(child.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);

}

}

截图:

二、请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识

实验代码为:

import javax.swing.*;

class AboutException {

   public static void main(String[] a) 

   {

      int i=1, j=0, k;

      k=i/j;

try

{

k = i/j;    // Causes division-by-zero exception

//throw new Exception("Hello.Exception!");

}

catch ( ArithmeticException e)

{

System.out.println("被0除.  "+ e.getMessage());

}

 catch (Exception e)

{

if (e instanceof ArithmeticException)

System.out.println("被0除");

else

{  

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

}

}

finally

     {

      JOptionPane.showConfirmDialog(null,"OK");

     }

  }

}

实验截图为:

使用Java异常处理的机制:

把可能会发生错误的代码放进try语句块中。当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。当异常发生时,程序控制流程由try语句块跳转到catch语句块。不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

三、多层的异常捕获-1

   阅读下面代码,写出程序的运行结果:

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

        } 

    } 

}

输出结果为:ArrayIndexOutOfBoundsException内层try-catch

   发生ArithmeticException

截图为:

四、写出下列程序的运行结果:

  public class CatchWho2 { 

    public static void main(String[] args) { 

        try {

           try { 

                throw new ArrayIndexOutOfBoundsException(); 

            } 

            catch(ArithmeticException 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"); 

        } 

原文地址:https://www.cnblogs.com/znn123/p/4967374.html