gpt4 book ai didi

java - Prevent some class to override the Object class's method in java ,困惑的答案

转载 作者:行者123 更新时间:2023-11-29 09:56:31 24 4
gpt4 key购买 nike

我在一些网站上发现了这个问题,比如“你会如何阻止某人覆盖 java.lang.Object 类?”但我无法理解任何回应。按照我的想法,它可能完成-

Can create a Base class and all the methods of Object class can be overriden and then we can throw some unsupported exception for each method , so further all subclasses won;t be able to access any of the methods of Object or there is much more better way to do it.

最佳答案

制作一个 FinalObject extends Object 类,该类覆盖每个 Object 方法并使它们成为 final

class FinalObject extends Object {
public final int hashCode() { return super.hashCode(); }
public final int equals(Object o) { return super.equals(o); }
...
}

关于java - Prevent some class to override the Object class's method in java ,困惑的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9928300/

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