博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现的方法中,抛的异常只能比父类或接口中的少(转)
阅读量:5895 次
发布时间:2019-06-19

本文共 2662 字,大约阅读时间需要 8 分钟。

实现的方法中,抛的异常只能比父类或接口中的少

import java.io.IOException;public interface IHello {    void say(String msg) throws TestException, IOException;}class IHelloImpl implements IHello {    @Override    public void say(String msg) throws IOException {        System.out.println("msg = [" + msg + "]");    }}

 

 

类StormyInning既扩展了基类Inning,又实现了接口Storm,而且基类Inning和接口Storm都声明了方法event():

package com.liush.chapter12;

 

//Overridden methods may throw only the exceptions specified in their

//base-class versions,or exceptions derived from the base-class exceptions.

 

class BaseballException extends Exception {}

class Foul extends BaseballException {}

class Strike extends BaseballException {}

abstract class Inning {

    public Inning() throws BaseballException {}

    public void event() throws BaseballException {

        // Doesen't actually have to throw anthing

    }

    public abstract void atBat() throws Strike,Foul;

    public void walk() {} //Throws no checked exceptions

}

 

class StormException extends Exception {}

class RainedOut extends StormException {}

class EventException extends StormException {}

class PopFoul extends Foul {}

 

interface Storm{

    public void event() throws EventException;

    public void rainHard() throws RainedOut;

}

 

public class StormyInning extends Inning implements Storm{

    //OK to add new exceptions for constructor, but you must

    //deal with the base constructor exceptions:

    public StormyInning() throws RainedOut,BaseballException {}

    public StormyInning(String s) throws Foul,BaseballException {}

    

    @Override

    public void rainHard() throws RainedOut {

        // TODO Auto-generated method stub

        

    }

    public void event() {System.out.println("Override the Inning.");}

    @Override

    public void atBat() throws Strike, Foul {

        // TODO Auto-generated method stub

        

    }

    public static void main(String[] args) {

        try{

            StormyInning si = new StormyInning();

            si.atBat();

        }

        catch(PopFoul e) {System.out.println("Pop foul");}

        catch(RainedOut e) {System.out.println("Rained out");}

        catch(BaseballException e) {System.out.println("Generic baseball exception");}

        

        try{

            Inning i = new StormyInning();

            Storm k=new StormyInning();

            i.event();

            k.event();

            i.atBat();

        }

        catch(Strike e) { System.out.println("Strike");}

        catch(Foul e) { System.out.println("Foul");}

        catch(RainedOut e) { System.out.println("Rained out");}

        catch(BaseballException e) { System.out.println("Generic baseball exception");}

        catch (EventException e) {    // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

说明:类StormyInning既继承了Inning基类,又实现了Storm接口,用基类来建立StormyInning对象时,根据向上转型,event()是被认为是从Inning中来:

而用接口Storm来建立StormyInning对象时,event()则是被认为是从Sorm中来的:

因此,它们抛出异常就决定于它们的基类和接口了:

 

http://blog.163.com/liu_sheng_han/blog/static/190591372201212394856740/

转载地址:http://fpxsx.baihongyu.com/

你可能感兴趣的文章
Hadoop生态圈-Kafka的完全分布式部署
查看>>
css的border的solid
查看>>
[MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 1
查看>>
jQuery自动完成点击html元素
查看>>
[算法]基于分区最近点算法的二维平面
查看>>
webpack多页应用架构系列(七):开发环境、生产环境傻傻分不清楚?
查看>>
笨办法学C 练习1:启用编译器
查看>>
树的总结--树的性质(树的深度) leetcode
查看>>
nagios短信报警(飞信fetion20080522004-linrh4)
查看>>
【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!...
查看>>
linux 将大文件分成小文件
查看>>
CCNA- 距离矢量路由协议学习
查看>>
jira 配置 LDAP 访问
查看>>
企业实践用户邮箱导入/导出(第2部分)
查看>>
我的友情链接
查看>>
如何学习Linux命令-初级篇
查看>>
从Oracle Public Yum为Oracle Linux建立本地的Yum源
查看>>
在 SELECT 查询中使用表表达式
查看>>
静态路由和默认路由
查看>>
谈一谈Spring-Mybatis在多数据源配置上的坑
查看>>