gpt4 book ai didi

java - 在可抛出对象中重载 getCause() 方法

转载 作者:行者123 更新时间:2023-12-02 07:13:46 33 4
gpt4 key购买 nike

如何在可抛出对象中重载 getCause() 方法?我有以下内容,但它似乎不起作用,因为它说它不能用字符串重载。

public class MyException extends RuntimeException   {
String cause;
MyException(String s) {
cause = s;
}
@Overwrite public String getCause() {
return cause;
}

最佳答案

拥有两个仅返回类型不同的方法是非法的。假设有人写道:

Object obj = myException.getCause();

这是完全合法的 java,编译器无法确定它是 String 版本还是 Throwable 版本。

同样,您不能替换父类(super class)签名,因为这也是完全合法的:

Throwable t = new MyException();
Throwable t0 = t.getCause();
//Returns String?!?!?!?

关于java - 在可抛出对象中重载 getCause() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209957/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com