English 中文(简体)
Java - 理解界面
原标题:Java - understanding interfaces

我读过爪哇的界面, 现在我想做一个界面, 让我可以创建像下面这样的构造:

public class MyClass implements View.OnTouchListener {
    //Implementation
    public boolean onTouch(View arg0, MotionEvent arg1) {
       /*SOME CODE*/    
       return true;
    }
}

所以我想要创建类似的界面( 类 ) 。 如果我不知道哪个类执行, 我如何从 < code> OnTouchliarer 界面中引用 < code> < touch 方法? 如果我决定将各个类与界面( 不同的图书馆) 分开, 我怎么知道哪个类执行这个界面? 希望您明白我的意思 。

非常感谢!

最佳回答

我觉得我理解你问题的一部分

如果我决定把各班与接口(不同的图书馆)分开, 我怎么知道哪班会安装这个接口(不同的图书馆)?

使用类似设置

MyClass myObject = new MyClass();
if(myObject instanceof myInterface){  //if the object implements the interface
    /*Do Something*/
}
else{
    /*Do Something Else Not Including the Method*/
}

保留单词的例子会告诉你 特定对象是否属于该类 是通过继承还是通过实施

David B. 回答你问题前半部分 呼叫天体上的Touch(arg0,arg1)

myObject.onTouch(arg0,arg1);

Just saw your edit--if you use the instanceof keyword, you can check if the given object implements the interface (which means it would have the onTouch method). See the above code block for such an implementation.

问题回答

执行接口的任何类必须超越接口中显示的方法, 这样您就可以在知道某类是否要执行您从接口对象调用的方法时轻松休息 。

要从 on Touch 调用 on Touch lististister 您只需在对象中传递到 Touch lististener 的型号 的引用,然后从那里传到

onTouchListener myOnTouchListener = new MyClass();
myOnTouchListener.onTouch(arg0, arg1);

关于你的问题的第二部分,我想

How can I invoke onTouch method from OnTouchListener interface if I don t know which class implements it?

界面不使用方法。 正如我所描述的那样, 他们组成了一个界面和开发者之间的合同, 无论什么物体执行这个界面, 它都会有以下方法, 并有以下的签名 。

MyClasss 中,您将引用 onTouch 与您使用其他函数的方式相同 — — 以 View MotionEvent 传递给它。

And how can I know which classes implements this interface if I decided to separate classes from interface (different libraries)?

Javadoc 是您在此的朋友( 作为开发者 ) 。 或者, 如果您在收藏上做了一些事情, 比如 < code> ArrayList 、 < code> Stack 或持有 Objects 的东西, 您可以使用以下语法来保证, 无论发生什么, 您使用的收藏只包含执行您的界面的对象 :

ArrayList<? extends View.OnTouchListener> myArrayList;




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...