Java Interface as argument to a method

空扰寡人 提交于 2020-05-28 06:53:09

问题


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:

  1. Objects of classes that implement an interface can be legally passed as arguments to methods where just the implemented interface is asked (without casting?).
  2. 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)
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!