Implementing an Interface的几个问题?

yeahnope 2004-04-15 07:59:43
A legal nonabstract implementing class has the following properties:
1)It must not declare any new checked exceptions for an implementation
method.
2)It must not declare any checked exceptions that are broader than the
exceptions declared in the interface method.
3)It may declare runtime exceptions on any interface method
implementation regardless of the interface declaration.
4)It must maintain the exact signature and return type of the methods it
implements (but does not have to declare the exceptions of the interface).

对于这4条不是很明白。checked exceptions是什么意思?这4条分别如何理解?
谢谢
...全文
91 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
panpan221 2004-04-19
  • 打赏
  • 举报
回复
学习一下
yeshucheng 2004-04-16
  • 打赏
  • 举报
回复
学习中,这个以前还真没好好注意呢。不错
Hodex 2004-04-16
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/article/25/25082.shtm
tiger_wkh52741 2004-04-16
  • 打赏
  • 举报
回复
接口中的方法当然不能定义了,接口中的方法不能有它的实体!!!
不象在抽象类中。可以有方法定义,也可以有抽象方法不能定义,
当然抽象方法要加个 abstract 修饰一下!!!
yeahnope 2004-04-16
  • 打赏
  • 举报
回复
class ChildException extends ParentException{}
interface AnInterface{
void method() throws ParentException{}
}

请问:chanceqw 接口中的方法可以定义吗?
chanceqw 2004-04-16
  • 打赏
  • 举报
回复
sorry, 写错了
interface AnInterface{
void method() throws ParentException;
}
chanceqw 2004-04-15
  • 打赏
  • 举报
回复
3)RuntimeException是一类特殊的违例,一般情况下是不需要特别处理的,
因为他们代表了一类编程中出现的错误,是编程者的设计不严谨造成的,比如数组下标越界
当然你也可以处理这类违例,因为Java已经假定任何时候都有可能发生这类违例
所以,自然,你在合适的地方加上这样的违例是允许的。

4)这个是关于interface实现的问题,是说你必须实现interface中所有定义的方法,
必须保证方法名称和返回类型都和interface中声明的保持一致。
但是你不必要按照接口抛出异常,实际上你可以什么也不抛出。这一点在2)中已有说明
chanceqw 2004-04-15
  • 打赏
  • 举报
回复
sorry,点错了
一条总的原则,在更具体的Class中的方法不能产生超出上层Class(父类,或者Interface)中出现的Exception
这点应该不难理解。因为,否则会出现程序意想不到的异常而导致程序中断

1)不能新产生Exception

abstract class AbstractClass{
void method(){}
}
public class ImpClass extends AbstractClass{
void method() throws NewException{} //这是不允许的
}

2)不能超出接口中的Exception
class ChildException extends ParentException{}
interface AnInterface{
void method() throws ParentException{}
}
public class ImpClass implements AnInterface{
void method() throws ChildException{}//这是允许的
//void method() throws ParentException{}//这也可以
//void method(){}//这也可以
//void method() throws FriendException{}//不允许
}
chanceqw 2004-04-15
  • 打赏
  • 举报
回复
1)不能新产生Exception
abstract class AbstractClass{
void method(){}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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