简体   繁体   English

Java接口作为方法的参数

[英]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: 如果我们坚持以下事实:

  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) 接口变量可以保存实现此接口的类的已创建对象,但BUT(无显式强制转换)只能访问接口方法,并且只能访问它们(即使对象可能是具有其他方法的类)
  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. 如果只需要一次,则可以使用new关键字和“ 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. 实例化一个匿名类,该匿名类自动实现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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM