我可以拥有一个空的Java类吗? [英] Can I have an empty Java class?

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

问题描述

我正在创建一个基于网格的游戏。

I'm creating a grid based game.

我需要实现一组在网格中随机位置的障碍。
我创建了一个抽象类 ALifeForm ,它包含网格中每个项目的常用方法。显然,抽象类无法初始化,所以我打算创建一个新类 AObstacle ,它将扩展 ALifeForm

I need to implement a set of obstacles that take random positions within the grid. I've created an abstract class ALifeForm, that holds the common methods for every item within the grid. Obviously, abstract classes can't be initialised, so I was going to create a new class AObstacle, which will extend ALifeForm.

唯一的问题是,我的 AObstacle 类不是专门的。它需要的所有方法都在 ALifeForm

Only issue is, my AObstacle class isn't specialised. All the methods it needs are within ALifeForm.

我可以有一个空课吗?
这是不好的编程习惯吗?如果是这样,我可以实现什么呢?

Can I have an empty class? Is it bad programming practice? And if so, what can I implement instead?

推荐答案

当然......

class AObstacle { }

(加上任何你正在使用的继承模型。)没有什么可以阻止你这样做。

(Plus whatever inheritance model you're using.) There's nothing stopping you from doing this.

请记住,并不是你真正的事情。正在定义。 类型是。该类只是用于描述类型的语言/语法结构。如果所描述的类型除了继承模型之外没有任何属性或操作,那么就没有别的东西可以添加到它。

Remember that a class isn't really a thing that you're defining. A type is. The class is just the language/syntax construct used to describe the type. If the type being described has no attributes or operations aside from the inheritance model, then there's nothing else to add to it.

虽然你添加一件事。你给它一个名字。听起来并不多,但定义具有特定名称的语义概念(特别是在静态类型环境中)非常重要。您的类型现在具有除系统中其他类型之外的身份。如果以后添加了某些内容,则可以添加它们而不进行重构和更改。

Though you are adding one thing. You're giving it a name. It doesn't sound like much, but defining a semantic concept with a concrete name (particularly in a statically typed environment) is very important. Your type now has an identity apart from other types in the system. If things are added to it later, there's a place to add them without refactorings and breaking changes.

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

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