是否有任何理由要在类中定义每个方法的抽象类? [英] Are there any reasons to have an abstract class with every method in the class defined?

查看:82
本文介绍了是否有任何理由要在类中定义每个方法的抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎抽象类意味着类的定义不完整,因此无法实例化。

It seems that an abstract class means the definition of the class is not complete and hence cannot be instantiated.

我看到一些带有摘要的简单Java代码定义了所有方法的类。然后我想知道,为什么他们把它作为抽象类而不是真正的类呢?他们这样做是为了让我们无法从这个抽象类中实例化吗?或者他们从定义所有内容的抽象类中获得其他好处?

And I saw some simple Java code which has an abstract class with all of the methods defined. Then I was wondering, why do they make it as an abstract class instead of a real class? Are they doing this so we cannot instantiate from this abstract class? Or they are getting other benefits from making an abstract class with everything defined?

推荐答案

即使所有方法都有可能在默认实现中,这些实现在应用程序的上下文中实际上并不是有意义的。这些方法可能只进​​行内部簿记,而实际有用的实现必须由派生类提供,它执行它需要做的事情,然后调用超类方法。

It is possible that even though all the methods had a default implementations, these implementations weren't actually meaningful in the context of the application. The methods might only do internal bookkeeping while the actually useful implementation must be provided by a derived class which does what it needs to do and then calls the superclass method.

但是,这只是猜测。你必须展示一个实际的例子来说明这个设计的原因。

However, this is just speculation. You would have to show an actual example to tell what could be the reason for this design.

举个例子,我们来看一个简单的游戏引擎吧。我的游戏中有很多不同的 GameObject

As an example, let's take a simple game engine. I have lot's of different GameObjects in my game.


  • 有些是可见的,所以我的基本类得到 draw()方法。但是可能存在不可见的对象,例如根本没有出现的触发区域,因此我将其实现为基类中的无操作。

  • Some are visible, so my basic class gets a draw() method. But there might be invisible objects like trigger areas which don't show up at all, so I implement it as a no-op in the base class.

有些人在遇到某些事情时会做一些事情,因此每个人都会获得碰撞(其他)方法。但是当它们像纯粹的视觉粒子效果碰撞时,有些人什么都不做,所以我也在基类中提供了一个no-op。

Some do something when they collide with something, so each one gets a collide(other) method. But some don't do anything when they collide like a purely visual particle effect, so I also provide a no-op in the base class.

有些做了每个游戏都会勾选,所以他们得到一个 update()方法。但有些物体,如墙壁,可能根本不会做任何事情。所以我也提供了一个no-op。

Some do something every game tick, so they get a update() method. But some objects, like a wall, might not do anything at all on their own. So I also provide a no-op for this.

那么当我有一个对象时我该怎么办看不见,它本身没有做任何事情,也没有与任何东西互动?在游戏中没有理由这样做。所以我创建了这个基本类 abstract 理论上你可以实例化它,因为所有方法都有一个实现,但几乎你没有理由这样做,当你尝试时,你误解了我的游戏引擎是如何工作的。

So what do I do when I have an object which is invisible, doesn't do anything on its own and doesn't interact with anything? There is no reason to have this in the game. So I make this basic class abstract. Theoretically you could instance it because all methods have an implementation, but practically you have no reason to ever do this, and when you try, you misunderstood how my game engine works.

这篇关于是否有任何理由要在类中定义每个方法的抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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