在同一个文件中有多个类是不好的做法吗? [英] Is it a bad practice to have multiple classes in the same file?

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

问题描述

我曾经为一个文件有一个类。例如 car.cs 有类 car 。但是当我编程更多的类,我想将它们添加到同一个文件。例如 car.cs 有类 car 类等。

I used to have one class for one file. For example car.cs has the class car. But as I program more classes, I would like to add them to the same file. For example car.cs has the class car and the door class, etc.

我的问题是好的Java,C#,PHP或任何其他语言。我应该尝试在同一个文件中没有多个类,还是确定?

My question is good for Java, C#, PHP or any other language. Should I try not having multiple class in the same file or is it ok?

推荐答案

我认为你应该尽量保留代码到每个文件一个类。

I think you should try to keep your code to 1 class per file.

我建议这样做,因为以后更容易找到你的类。此外,它将更好地与您的源控制系统(如果一个文件更改,那么你知道一个特定的类已经改变)。

I suggest this because it will be easier to find your class later. Also, it will work better with your source control system (if a file changes, then you know that a particular class has changed).

我认为每个文件使用多个类是正确的,当你使用内部类...但内部类是在另一个类中,因此可以留在同一个文件中。内部类角色与外部类强烈相关,因此将它们放在同一个文件中是很好的。

The only time I think it's correct to use more than one class per file is when you are using internal classes... but internal classes are inside another class, and thus can be left inside the same file. The inner classes roles are strongly related to the outer classes, so placing them in the same file is fine.

这篇关于在同一个文件中有多个类是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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