A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lily15 中级黑马   /  2015-4-20 22:31  /  799 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lily15 于 2015-4-21 15:17 编辑

为什么在接口中的方法上声明异常,没有出现编译错误?

interface Shape
{
        void getArea() throws Exception;
        
}

class Rectangle implements Shape
{
        private int len,wid;
        Rectangle(int len,int wid)
        {
                if(len<=0||wid<=0)
                        throw new NoValueException("出现非法");
                this.len=len;
                this.wid=wid;
        }
        public void getArea()
        {
                System.out.println(len*wid);
        }
}

2 个回复

倒序浏览
哇 牛逼哄哄 ,,     try 呢 都没看到只看到throw
回复 使用道具 举报
我知道了,父类方法声明了异常,子类的覆盖方法可以不抛的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马