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

查看:31
本文介绍了我可以有一个空的 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天全站免登陆