gpt4 book ai didi

java - actionPerformed 中的 CloneNotSupportedException

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:51 25 4
gpt4 key购买 nike

在我的actionPerformed方法中,我调用克隆对象的“copy()”,但编译器给我这个错误:“java.awt.event.ActionListener;重写的方法不会抛出java.lang.CloneNotSupportedException”,我该怎么办?

        public void actionPerformed(ActionEvent e){
if (e.getSource() instanceof JButton) {
copy(); ...

谢谢

最佳答案

You can not add throwed checked exception to a method while overriding it.

[...] the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. [...]

你必须处理它。

@Override
public void actionPerformed(ActionEvent e) {
//code
try {
copy();
} catch (CloneNotSupportedException cnse) {
cnse.printStackTrace();
}
}

关于java - actionPerformed 中的 CloneNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230042/

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