[英]Java Interface as argument to a method
In some Java methods I see that an Interface argument is required. 在某些Java方法中,我看到需要一个Interface参数。 Since an Interface cannot be instantiated, does that mean that an object of every class that implements this interface can be passed as argument?
由于无法实例化接口,这是否意味着可以将实现该接口的每个类的对象作为参数传递?
For example in the setOnClickListener method of the ListView class in Android, 例如,在Android中ListView类的setOnClickListener方法中,
setOnItemClickListener(AdapterView.OnItemClickListener listener)
the OnItemClickListener interface (nested in the AdapterView abstract class) is needed as an argument. 需要OnItemClickListener接口(位于AdapterView抽象类中)作为参数。
What kind of object is required to be passed here? 这里需要传递哪种对象?
Thank you in advance. 先感谢您。
Yes - if an interface type is required as an argument, you can pass any object whose class implements this interface. 是的-如果需要接口类型作为参数,则可以传递其类实现此接口的任何对象。
Example: 例:
// the method
void myMethod(MyInterface object) { ... }
// the interface
interface MyInterface {
void interfaceMethod();
}
// class implementing the interface
class MyImplementation implements MyInterface {
void interfaceMethod() {
// ...
}
}
You can now do this 您现在可以执行此操作
MyInterface object = new MyImplementation();
myMethod(object);
Hope this helps! 希望这可以帮助!
What Is an Interface? 什么是接口? gives a nice explaination
给出了很好的解释
Implementing an interface allows a class to become more formal about the behavior it promises to provide.
实现一个接口可使类对其承诺提供的行为变得更加正式。
So you can implement any class with the interface you need and provide an object of this class as listener. 因此,您可以使用所需的接口实现任何类,并提供该类的对象作为侦听器。 Also you could implement the interface as anonymous inner class like it is done broadly within Android development.
您也可以将接口实现为匿名内部类,就像在Android开发中广泛完成的那样。
setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
});
Well, it seems that solidly formulating a question helps one to digest it better. 好吧,扎实提出一个问题似乎可以帮助人们更好地理解它。
If we hold to the following facts: 如果我们坚持以下事实:
So it seems that in my case, the following code: 因此,在我看来,以下代码:
setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
}); });
instantiates an anonymous class that automatically implements the AdapterView.OnItemClickListener interface, overrides in-line the interface onItemClick() method and passes the object to the setOnItemClickListener function. 实例化一个匿名类,该匿名类自动实现AdapterView.OnItemClickListener接口,内联重写接口onItemClick()方法,并将该对象传递给setOnItemClickListener函数。
To make it even clearer, the code above could have been written: 为了更加清楚,可以编写上面的代码:
class classDef implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
}
classDef myClass = new classDef();
setOnItemClickListener(myClass);
Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.