我刚刚学习这个界面,现在面临着如何在界面内提供内容的问题。
我读了这篇文章 http://pixelscientists.com/blog/posts/drag-and-drop-inventory-with-libgdx-part-i
它的内容让我思考一些事情 - 当他使用其中的界面时
public interface SlotListener {
void hasChanged(Slot slot);
}
他也创造了这个
private void notifyListeners() {
for (SlotListener slotListener : slotListeners) {
slotListener.hasChanged(this);//this is Slot class
}
}
如何为 hasChanged() 方法提供内容? 我真的不明白这个界面的想法...... 我的意思是, hasChanged() 中没有内容(当然因为它是接口(interface)),但为什么它又指向 Slot 类?该方法需要什么内容以及如何进行?
也许您需要查看上面有关此问题的链接。
我已经通过 Google 搜索来了解界面,但我只找到了基本教程,没有找到这样的案例。我也已经在一些社区询问过这个问题,但没有结果。
最佳答案
接口(interface)
不是真正的类。它只是有一些方法签名。真正的类通过实现接口(interface)
中的所有方法来实现接口(interface)
。
SlotListener 是一个接口(interface)。当您想实现此接口(interface)时,您应该创建一个具有 hasChanged(Slot slot)
方法的类。
public class realClass implements SlotListener {
public void hasChanged(Slot slot){
//Some code here...
}
}
阅读有关接口(interface)的更多信息。 Interfaces Tutorial by Oracle
关于java - 在 Interface Java 中设置内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551749/