java - 为什么超类方法可以在超类方法中声明不异常?
问题描述
我正在阅读 Kathey sierra 的 SCJP 6 书,并遇到了在覆盖方法中抛出异常的解释。我完全没听懂。谁能向我解释为什么我们不能使用 Dog2 对象在 Animal 引用上调用 eat() 方法?
class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { /* no Exceptions */}
public static void main(String [] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error - unreported exception
}
}
这个问题可能是重复的,但该问题也没有给出答案。有人可以解释编译器的行为吗?
解决方案
推荐阅读
- ssas - SSAS 表格 DMV 获取表的源查询
- c# - 使用 App.Config 派生要使用的类/变量
- javascript - JavaScript:关于从字符串到整数的转换方式的性能
- python-3.x - 我正在尝试使用 'IF' 和 'OR' 函数在新列中具有值
- postgresql - 如何通过 graphql 和 typeorm 更新或创建实体(有关系)
- flutter - 问题 SingleChildScrollView 与 PageView 颤动
- angular - 浏览器中的安全 https 标头和响应(API 调用)
- image - 使用 ImageMagick 分割巨大的 PNG 图像文件
- safari - 伪元素动画在 Safari 和 iOS 中不起作用
- python - 简单的 2 人井字游戏 Python