一个让人困扰的问题

zenghuiss 2009-10-20 07:21:35
interface Type1 {
void f() throws CloneNotSupportedException;
}

interface Type2 {
void f() throws InterruptedException;
}

interface Type3 extends Type1, Type2 {
}

public class Arcane3 implements Type3 {
public void f() {
System.out.println("Hello world");
}
public static void main(String[] args) {
Type3 t3 = new Arcane3();
t3.f();
}
}

这个程序编译过了。。也能执行出结果。。
有点颠覆我以前的理解了 Type3怎么能继承两个 望解释详细点
还有。。这个里面异常的问题也望能解释解释
...全文
86 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
LC3TWZ 2009-10-21
  • 打赏
  • 举报
回复
接口内的继承是一抛出最小的异常为首要的
zenghuiss 2009-10-21
  • 打赏
  • 举报
回复
查资料时明白了
不过还是谢谢各位的帮助
lzh_me 2009-10-21
  • 打赏
  • 举报
回复
学习了,尤其是1楼的,抛出的是异常的交集而非并集,更深刻的理解了抛出异常应该从“范围”小的异常开始。
zhaoruifeng_good 2009-10-20
  • 打赏
  • 举报
回复
java里的类只能够单继承类, 多实现接口

接口可以继承多个 这在java里的说法叫接口的扩展 没有任何问题

阿士匹灵 2009-10-20
  • 打赏
  • 举报
回复
学习 帮顶

不能改变结果 就去完善过程
  • 打赏
  • 举报
回复
java中的接口是可以多继承的。类不可以。
fengyud 2009-10-20
  • 打赏
  • 举报
回复
嗯,这个问题问的好。
ddshuaige 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangwei5155 的回复:]
其实不需要用Type3,直接写成public class Arcane3 implements Type1,Type2{}
也一样的。
"interface Type3 extends Type1, Type2 { } "
这里有点问题:能继承两个接口吗?

[/Quote]

这里没问题。接口支持多继承。
wangwei5155 2009-10-20
  • 打赏
  • 举报
回复
其实不需要用Type3,直接写成public class Arcane3 implements Type1,Type2{}
也一样的。
"interface Type3 extends Type1, Type2 { } "
这里有点问题:能继承两个接口吗?
Quebradawill 2009-10-20
  • 打赏
  • 举报
回复
接口可以继承多个,但是Type1和Type2的方法名都相同,应该有问题的吧?
若鱼1919 2009-10-20
  • 打赏
  • 举报
回复
1.接口可以多继承
3.Each interface limits the set of checked exceptions that method f can throw. The set of checked exceptions that a method can throw is the intersection of the sets of checked exceptions that it is declared to throw in all applicable types, not the union. As a result, the f method on an object whose static type is Type3 can't throw any checked exceptions at all. Therefore, Arcane3 compiles without error and prints Hello world.
也就是可以抛出的是异常的交集,而不是并集。
参考:java puzzlers-----Puzzle 37: Exceptionally Arcane

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧