在哪里放置内部类? [英] Where to put inner classes?

查看:171
本文介绍了在哪里放置内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我会在文件的底部声明内部类对他们的实现不感兴趣,只想得到你的主类的方法,所以他们不应该妨碍。

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.

这篇关于在哪里放置内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆