且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以有一个空的 Java 类吗?

更新时间:2023-01-19 18:55:17

当然...

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.