gpt4 book ai didi

java - 我应该将 Java 中的静态嵌套类重构为单独的类吗?

转载 作者:行者123 更新时间:2023-12-04 00:38:56 25 4
gpt4 key购买 nike

我继承了包含静态嵌套类的代码:

public class Foo {

// Foo fields and functions
// ...
private static class SGroup {
private static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();

public SGroup(int id, String type) {
// ...
}
}
}

从阅读 SO(例如 Java inner class and static nested class )我相信这相当于两个单独文件中的两个单独类:
 public class Foo {

// Foo fields and functions
// ...
}


public class SGroup {
static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();

public SGroup(int id, String type) {
// ...
}
}

如果这是正确的,维护静态嵌套类结构有什么好处还是应该重构?

最佳答案

这取决于类的用途。如果它耦合到外部类,例如,就像 Map.Entry 一样,只需将其保留。但是,如果使用没有其封闭类型的类是有意义的,您也可以将其提升为顶级类。

关于java - 我应该将 Java 中的静态嵌套类重构为单独的类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1581931/

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