这是使用java关键字“interface”的正确位置吗? [英] Would this be the correct place to use the java keyword "interface"?

查看:103
本文介绍了这是使用java关键字“interface”的正确位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java很新。在阅读了一些关于路径查找的信息之后,我读到了使用空类作为接口的未知对象类型。

I'm rather new to Java. After just reading some info on path finding, I read about using an empty class as an "interface", for an unknown object type.

我正在开发一个基于医院主题的Java游戏。到目前为止,用户可以建立一个接待台和一个GP的办公室。它们是两种不同类型的对象,一种是 Building ,另一种是 ReceptionDesk 。 (在我的班级结构中。)

I'm developing a game in Java based on hospital theme. So far, the user can build a reception desk and a GP's office. They are two different types of object, one is a Building and one is a ReceptionDesk. (In my class structure.)

我的班级结构如下:

GridObject-->Building
GridObject-->Item-->usableItem-->ReceptionDesk.

当可用项目可以旋转且建筑物不能旋转时,问题就出现了。鼠标单击事件位于网格上,因此调用相同的方法。 GP的办公室是 Building ,前台是 ReceptionDesk 。只有 ReceptionDesk 的方法 rotate 。当右键单击网格时,如果处于构建模式,我必须使用此if语句:

The problem comes when the usable item can be rotated and the building cannot. The mouse click event is on the grid, so calls the same method. The GP's office is a Building and the reception desk is a ReceptionDesk. Only the ReceptionDesk has the method rotate. When right clicking on the grid, if in building mode, I have to use this "if" statement:

if (currentBuilding.getClass.equals(ReceptionDesk.getClass)

然后我必须创建一个新的 ReceptionDesk ,使用 rotate 方法,并将
接待台放回 currentBuilding GridObject

I then have to create a new ReceptionDesk, use the rotate method, and the put that reception desk back into the currentBuilding GridObject.

我不确定自己是否能很好地解释这个问题。抱歉。我还是Java新手。我会的尝试回答任何问题,如果需要我可以发布更多的代码snippits。我不知道可能有一种方法可以绕过不知道对象的类的问题,但是我也可能以错误的方式去做。

I'm not sure if I'm explaining myself very well with this question. Sorry. I am still quite new to Java. I will try to answer any questions and I can post more code snippits if need be. I didn't know that there might be a way around the issue of not knowing the class of the object, however I may also be going about it the wrong way.

在我看到这个网站上的回复有多快和有帮助之前,我没有计划对此进行调查!:)

I hadn't planned on looking into this until I saw how fast and helpful the replies on this site were! :)

提前致谢。

Rel

推荐答案

你不喜欢在你的案例中做一些事情之前,我想检查一个对象的类。您应该使用 polymorphism 。您希望让Interface定义一些方法。每个类都实现这些方法。通过它的接口引用对象,让这些对象的各个实现将它们的值返回给调用者。

You don't want to check the class of an object before doing something with it in your case. You should be using polymorphism. You want to have the Interface define some methods. Each class implement those methods. Refer to the object by its interface, and have the individual implementations of those objects return their values to the caller.

如果你描述了一些你认为对象的对象需要,这里的人会就你应该如何安排它们发表意见。但是根据您提供的内容,您可能需要一个构建界面来定义一些常规方法。您可能还需要UsableItem界面或更通用的界面。医院可以是一个实施建筑的阶层。 ReceptionDesk可以实现UsableItem。构建可以在其中包含一个UsableItem网格。

If you describe a few more of the objects you think you need, people here will have opinions on how you should lay them out. But from what you've provided, you may want a "Building" interface that defines some general methods. You may also want a "UsableItem" interface or something more generic. Hospital could be a class that implements building. ReceptionDesk could implement UsableItem. Building could have a grid of UsableItem inside it.

如果rotate()是实际完成某些工作的所有家具的常用方法,您可以考虑创建一个AbstractUsableItem类,是一个实现UsableItemand的抽象类,提供了rotate()方法。如果每个实现类中的rotate都不同,那么你可以在界面中使用该方法,但每个类(如ReceptionDesk)都会使用rotate()方法做自己的事情。您的代码将执行以下操作:

If rotate() was a common method to all furniture that actually did some work, you may consider making an AbstractUsableItem class that was an abstract class implementing UsableItemand providing the rotate() method. If rotate was different in each implementing class, you would have that method in the interface, but each class, like ReceptionDesk would do its own thing with the rotate() method. Your code would do something like:

UsableItem desk = new ReceptionDesk();
desk.rotate()

在你的例子中,如果你的鼠标点击屏幕旋转在它下面的对象,你确实需要检查是否可以在做这样的事情之前旋转对象,你会做什么

In your example, if your mouse click on a screen rotated the object under it, and you really did need to check to see if the object could be rotated before doing something like that, you'd do

if (clickedObject instanceOf UsableItem) {
  ((UsableItem) clickedObject).rotate();
}

其中UsableItem是接口或抽象类。有些人认为所有的设计都应该通过接口合同完成,并建议每种类型的接口,但我不知道你是否必须走这么远。

where UsableItem was the interface or abstract class. Some people feel that all design should be done via an interface contract and suggest an interface for every type of class, but I don't know if you have to go that far.

这篇关于这是使用java关键字“interface”的正确位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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