嵌套类与单独的类文件 [英] Nested classes vs Separate class files

查看:132
本文介绍了嵌套类与单独的类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少在java中,我的编译器不会让我把两个(或更多)单独的类放在一个类文件中。它迫使我在同一个包中创建多个文件。我明白为什么会是这样,我同意这个原则。但是,我已经意识到,我可以轻松地嵌套类,并在一个文件中包含整个程序(排除导入的库)。我想象这会被皱眉,但我不明白为什么。什么是嵌套类的危险和缺点,而不是标准系统(在组织和整洁的严重损失之外)?

解决方案

p>好吧,如果你把所有的东西保存在同一个文件中,你会丢失包,private关键字不再有任何意义。



由于大多数Java项目将在一个IDE中打开,该IDE将不同的类显示为不同的选项卡,因此您可以在程序中加入更多的光标。



为什么会有同一个文件中的所有内容?


In java at least, my compiler won't let me put two (or more) separate classes in a single class file. It forces me to create multiple files in the same package. I understand why this is, and I agree with this principle. However, I've realized that I could just as easily nest the classes and contain the entire program (excluding imported libraries) in a single file. I would imagine that this would be frowned upon, but I don't understand why. What are the dangers and disadvantages of nesting class as opposed to the standard system (outside of the severe loss in organization and neatness)?

解决方案

Well, if you keep everything in the same file, you lose packages and the private keyword does no longer have any meaning.

Given that most Java projects will be opened in an IDE that presents different classes as different tabs, you can have more that one cursor in the program.

Why would you have everything in the same file?

这篇关于嵌套类与单独的类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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