Can I have an empty Java class?

前端 未结 2 1427
暗喜
暗喜 2021-01-06 16:38

I\'m creating a grid based game.

I need to implement a set of obstacles that take random positions within the grid. I\'ve created an abstract class ALifeForm

2条回答
  •  孤城傲影
    2021-01-06 17:01

    Of course...

    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.

提交回复
热议问题