动手动脑接口与继承

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("抱歉!您的操作有误!请重新输入");

}

}

}

 

}

 

转载于:https://www.cnblogs.com/1443188449qq/p/4965102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值