一个java文件可以有多个类吗? [英] Can a java file have more than one class?

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

问题描述

在Java文件中拥有多个类的目的是什么?我是Java的新手。

What is the purpose of having more than one class in a Java file ? I am new to Java.

编辑:
这可以通过在公共类中创建一个内部类来实现,对吧?

Edited: That can be achieved by creating a inner class inside a public class, right?

推荐答案

是的,可以。但是,每个.java文件只能有一个 public 类,因为公共类必须与源文件具有相同的名称。

Yes, it can. However, there can only be one public class per .java file, as public classes must have the same name as the source file.

在一个源文件中包括多个类的目的是将相关的支持功能(内部数据结构,支持类等)与主公共类捆绑在一起。请注意,不要这样做,总是确定 - 唯一的效果是对代码的可读性(或不)。

The purpose of including multiple classes in one source file is to bundle related support functionality (internal data structures, support classes, etc) together with the main public class. Note that it is always ok not to do this -- the only effect is on the readability (or not) of your code.

这篇关于一个java文件可以有多个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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