Java:一个文件中的多个类声明 [英] Java: Multiple class declarations in one file

查看:37
本文介绍了Java:一个文件中的多个类声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,您可以在单个文件中定义多个顶级类,前提是其中最多一个是公开的(请参阅 JLS §7.6).参见下面的例子.

In Java, you can define multiple top level classes in a single file, providing that at most one of these is public (see JLS §7.6). See below for example.

  1. 这种技术是否有一个简洁的名称(类似于innernestedanonymous)?

JLS 表示系统可能强制限制这些二级类不能被包的其他编译单元中的代码引用,例如,它们不能被视为包私有的.这真的会在 Java 实现之间发生变化吗?

The JLS says the system may enforce the restriction that these secondary classes can't be referred to by code in other compilation units of the package, e.g., they can't be treated as package-private. Is that really something that changes between Java implementations?

例如,PublicClass.java:

e.g., PublicClass.java:

package com.example.multiple;

public class PublicClass {
    PrivateImpl impl = new PrivateImpl();
}

class PrivateImpl {
    int implementationData;
}

推荐答案

我建议将此技术(包括单个源文件中的多个顶级类)命名为混乱".说真的,我认为这不是一个好主意 - 在这种情况下我会使用嵌套类型.然后仍然很容易预测它在哪个源文件中.不过,我认为这种方法没有官方术语.

My suggested name for this technique (including multiple top-level classes in a single source file) would be "mess". Seriously, I don't think it's a good idea - I'd use a nested type in this situation instead. Then it's still easy to predict which source file it's in. I don't believe there's an official term for this approach though.

至于这是否真的在实现之间发生变化 - 我非常怀疑,但如果你首先避免这样做,你就永远不需要关心:)

As for whether this actually changes between implementations - I highly doubt it, but if you avoid doing it in the first place, you'll never need to care :)

这篇关于Java:一个文件中的多个类声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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