您的位置:首页 > 其它

动手动脑接口与继承

2015-11-14 21:23 489 查看
(1)可以使用instanceof运算符判断一个对象是否可以转换为指定的类型:

参看实例: TestInstanceof.java

public class TestInstanceof

{

public static void main(String[] args)

{

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

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

Object hello = "Hello";

//String是Object类的子类,所以返回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("抱歉!您的操作有误!请重新输入");

}

}

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: