问题
In some Java methods I see that an Interface argument is required. 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,
setOnItemClickListener(AdapterView.OnItemClickListener listener)
the OnItemClickListener interface (nested in the AdapterView abstract class) is needed as an argument.
What kind of object is required to be passed here?
Thank you in advance.
回答1:
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!
回答2:
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.
setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
....
}
});
回答3:
Well, it seems that solidly formulating a question helps one to digest it better.
If we hold to the following facts:
- Objects of classes that implement an interface can be legally passed as arguments to methods where just the implemented interface is asked (without casting?).
- Interface variables can hold created objects of classes that implement this interface BUT (without explicit casting) have only access to the interface methods and only them (even though the object may be of a class with additional methods)
- If only needed once, objects of anonymous classes can be created on-the-fly where an interface is required by using the new keyword and the "Interface constructor" and automatically implement that interface.
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.
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.
来源:https://stackoverflow.com/questions/40207720/java-interface-as-argument-to-a-method