放置包类java的最佳位置

发布于 2024-12-13 03:45:47 字数 274 浏览 1 评论 0原文

我有一个游戏引擎类,由客户端程序和服务器程序共享。我有一个特定的函数updatePlayer(PlayerData data),它应该更新播放器。 (PlayerData 基本上是一种数据包数据类型。)

为了获得最佳标准,PlayerData 类应该放在哪里?它应该是它自己的文件、网络包中的文件,还是静态 Packet 类的子类?通常,我会在 c++ 中创建一个主命名空间(以我的项目命名),或者在我的网络类中定义该类,但你不能在 java 中的一个文件中定义多个类。

你会怎么做?

I have a game engine class, shared by the client program and the server program. I have a particular function updatePlayer(PlayerData data) which should update a player. (PlayerData is basically a packet datatype.)

For best standards, where should the PlayerData class go? Should it be it's own file, in a networking package, or a subclass of a static Packet class? Normally, I'd make a master namespace(named after my project) in c++ or define the class in my networking class, but you can't define multiple classes in one file in java.

How would you do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-12-20 03:45:47

为了达到最佳标准,PlayerData 类应该放在哪里?

它绝对应该在自己的文件中。 Java 中通常不需要嵌套/内部类。

For best standards, where should the PlayerData class go?

It should absolutely be in its own file. Nested/inner classes are generally unnecessary in Java.

向地狱狂奔 2024-12-20 03:45:47

我不确定在这种情况下“数据包”是什么意思。如果我的回答似乎不适用,请澄清。

但一般来说,在Java中如果你有几个相关的类,你应该创建一个包并将相关的类放在该包中。 Java 中的“包”实际上只是一个包含所有相关 .java 文件的子目录。

不要仅仅为了对事物进行分组而创建子类。那不是他们的目的。

如果您来自另一种语言,则必须为每个类创建单独的文件似乎很痛苦。当然,这可能意味着您必须打开十个文件才能查看所有相关内容。但另一方面,它可以轻松找到正确的文件:只需查找具有该名称的文件即可。由于文件名和类名必须始终相同,因此我们从必须理解的事物列表中删除一个名称。我对 VB 有一些小烦恼,其中文件名和类名略有不同,这导致我不得不多花几分钟才能弄清楚。

I'm not sure what you mean by a "packet" in this context. If my answer seems inapplicable, please clarify.

But in general, in Java if you have several related classes, you should create a package and put the related classes in that package. A "package" in Java is really just a subdirectory that includes all the related .java files.

Don't create a subclass just to group things. That's not what they're for.

If you are coming from another language it might seem like a pain to have to create a separate file for every class. Sure, this can mean that you have to open ten files to see all the related stuff. But on the other hand, it makes it easy to find the right file: just look for the file with that name. And as the file name and the class name must always be the same, we eliminate one name from the list of things we have to comprehend. I've had some minor annoyances with VB where a file name and a class name are subtly different and it's caused me to have to take a few extra minutes to figure it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文