zoukankan      html  css  js  c++  java
  • 动手动脑接口与继承

    1)可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:

    参看实例: TestInstanceof.java

    public class TestInstanceof

    {

    public static void main(String[] args) 

    {

    //声明hello时使用Object类,则hello的编译类型是ObjectObject是所有类的父类

    //hello变量的实际类型是String

    Object hello = "Hello";

    //StringObject类的子类,所以返回true

    System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));

    //返回true

    System.out.println("字符串是否是String类的实例:" + (hello instanceof String));

    //返回false

    System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));

    //String实现了Comparable接口,所以返回true

    System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));

    String a = "Hello";

    //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过

    //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));

    }

    }

    2)下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

    m=d;

    d=m;

    d=(Dog)m;

    d=c;

    c=(Cat)m;

    先进行自我判断,得出结论后,运行TestCast.java实例代码,看看你的判断是否正确

    判断:d=m会出错,因为基类对象不能直接赋给子类的变量;d=c会出错,因为谈们没有关系,不能赋值;c=(Cat)m会出错,因为之前进行强制类型转换了。

    (3)动手动脑:请阅读并运行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");

         }

      }

    }

    运行结果:

    (3)动手动脑:多层的异常捕获-1

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

    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)动手动脑:多层的异常捕获-2

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

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

            } 

        } 

    }

    (3)辨析:finally语句块一定会执行吗?

     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语句块不一定会执行。

    7)编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

    要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

    源代码:

    import java.util.Scanner;

    public class Score {

    public static void main(String[] args){

    // TODO 自动生成的方法存根

    for(;;)

    {try

    {

    System.out.println("请输入成绩:");

    Scanner s=new Scanner(System.in);

    int sc=s.nextInt();

    if(sc>=0&&sc<=100)

    {

    if(sc<60){System.out.println("不及格!");break;}

    if(sc<70&&sc>=60){System.out.println("及格!");break;}

    if(sc<80&&sc>=70){System.out.println("中!");break;}

    if(sc<90&&sc>=80){System.out.println("良!");break;}

    if(sc<=100&&sc>=90){System.out.println("优!");break;}

    }

    else

    System.out.println("您输入的成绩有误!请重新输入");

    }

    catch(Exception e)

    {

    System.out.println("抱歉!您的操作有误!请重新输入");

    }

    }

    }

    }

  • 相关阅读:
    学习进度条
    0302我的感想
    1217实验四 递归下降语法分析程序设计
    1118 实验三 有限自动机的构造与识别
    1112我的访问与评论日记
    1014 我的C语言文法定义与C程序推导过程
    0917词法分析
    命令解释程序的编写
    构建之法前三章读后感
    复利计算4.0
  • 原文地址:https://www.cnblogs.com/1443188449qq/p/4965102.html
Copyright © 2011-2022 走看看