java 内部类放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4730126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:45:56  来源:igfitidea点击:

Where to put inner classes?

javacoding-styleinner-classes

提问by Jason Noack

Some might like to argue that this is a candidate for the least important issue of all times. Yet code style is a very important topic for me, and I want to ensure that I write code in a readable way - for me and the majority of developers.

有些人可能会争辩说,这是有史以来最不重要的问题的候选者。然而,代码风格对我来说是一个非常重要的话题,我想确保我以可读的方式编写代码 - 对于我和大多数开发人员。

That's why I'm wondering where you guys are declaring your inner classes.

这就是为什么我想知道你们在哪里声明你的内部类。

I'm following the following method ordering scheme, because it is quite common:

我遵循以下方法排序方案,因为它很常见:

public void foo() {
    usedByFoo();
}

private void usedByFoo() {
}

public void bar() {
}

I order them from top to bottom, every method as close to where it is used.

我从上到下对它们进行排序,每种方法都尽可能靠近使用地点。

Now I could do the same with inner classes, like this:

现在我可以对内部类做同样的事情,就像这样:

class Outer {
    private Inner inner;

    private class Inner {};

    public Outer() {
    }

    ...
}

I think this is the most consistent style to follow for me, but I've also often seen people declare all inner classes either at the top or at the bottom of the file.

我认为这是我遵循的最一致的风格,但我也经常看到人们在文件的顶部或底部声明所有内部类。

Which style should I follow, given my way of ordering methods? What is the most common way to do this?

考虑到我的订购方法,我应该遵循哪种风格?执行此操作的最常见方法是什么?

回答by Amir Rachum

I would declare inner-classes in the bottom of the file - usually you're not interested in their implementations and just want to get to your main class' methods, so they shouldn't get in the way.

我会在文件底部声明内部类——通常你对它们的实现不感兴趣,只想访问你的主类的方法,所以它们不应该妨碍。

回答by Michael Berry

My preferred style is to put them wherever they seem to make most sense. Usually this is at the bottom so they're out the way, but sometimes I find it makes more sense to put them before a certain group of methods (if these are the methods that use the inner class.)

我更喜欢的风格是把它们放在它们看起来最有意义的地方。通常这是在底部,所以它们不在路上,但有时我发现将它们放在特定的一组方法之前更有意义(如果这些是使用内部类的方法。)

If the class gets too unwieldy with loads of methods and inner classes though, it's probably a bad design choice (cohesion is too low.) I've sometimes let classes get this way by accident and they're horrible to deal with later - these days if I can see one going that way I'll generally refactor it out, perhaps even into its own package. If you get to the point where you've got so many inner classes you don't know what to do with them, I'd take this approach. There's even some that advise against using inner classes at all for this reason (though I disagree - they're a valuable resource when used properly, you just need to take care they don't get out of hand.

如果类在大量方法和内部类中变得过于笨拙,那么这可能是一个糟糕的设计选择(内聚度太低。)我有时会偶然让类以这种方式获得这种方式,并且以后处理它们很糟糕 - 这些几天后,如果我能看到有人这样做,我通常会重构它,甚至可能会重构到它自己的包中。如果您到了有这么多内部类不知道如何处理它们的地步,我会采用这种方法。甚至有些人出于这个原因建议完全不要使用内部类(尽管我不同意 - 如果使用得当,它们是一种宝贵的资源,您只需要注意它们不会失控。