在类构造函数中调用的接口方法

时间:2013-07-05 02:02:23

标签: java

有人可以在下面的例子中解释为什么在类构造函数中作为参数传递接口方法时可以直接调用它?我尝试在Java语言规范中搜索规则但找不到。

public interface Interface {
    public void foo();
}

public class Main {
    public Main() {}
    public Main(Interface obj) {obj.foo();}
    public static int test() {return 123;}
}

4 个答案:

答案 0 :(得分:3)

只是一种多态行为,Java期望实现该接口的方法。 这意味着,任何实现该方法的类都是Interface,因此您可以拥有该方法的许多不同实现。 让我们说:

public class ImplementedInterface implements Interface
{
    public void foo()
    {
        System.out.println("Hey!, i'm implemented!!");
    }
}

所以当你打电话时:

Interface aux = new ImplementedInterface();
Main m = new Main(aux);

文字"嘿!,我已实施!!"将被打印。

答案 1 :(得分:1)

您可以从foo引用调用Interface方法,因为它只能包含实现Interface的类的对象,因此它将为foo方法提供正文。

现在感谢后期绑定Java将在需要时使用对象类的代码。

答案 2 :(得分:1)

我认为你很困惑,你认为它是接口类型它是一个接口

public Main(Interface obj) {
  obj.foo();
}

obj是Interface的具体实现中的对象。

您可能希望看到采用此方法的一些常见设计模式,例如Strategy Pattern

例如:

public interface Searcher {
  void search(String text, List<String> words);
}

public class BinarySearcher implements Searcher{
   @Override
   public void search(String text , List<String> words){
      //code here
   }

}

public class LinearSearcher implements Searcher{
     @Override 
     public void search(String text ,List<String> words ){
           // code here
     }
}

public class WordContext {

private Searcher searcher;
private List<String> words;

public void makeSearch(String text){
    searcher.search(); // you only know at runtime what subtype will be searcher
}

// here you inject by contract
public void setSearcher(Searcher searcher){
  this.searcher= searcher;
}

// here you inject by contract
public void setWords(List<String> words){
   this.words = words;
}

}

这是您通过抽象合同而非具体实施指导的主要优势。 在这个例子中你可以改变搜索它的搜索者,可以是linearSearcher或binarySearcher,这就是多态魔法!

答案 3 :(得分:0)

这是Programming to an interface, not an implementation发挥作用的地方。您的方法期望该类的对象实现interface

我会用一个例子来解释它。

让我们说我有     LinkedList<String> ll = new LinkedList<String>();

我有     ArrayList<String> al = new ArrayList<String>();

现在我有一个方法 -

public void deleteFirst(List aList) {
  System.out.println(aList.remove(0));
 }

现在,您可以将llal都传递给deleteFirst方法。这意味着您的方法将传递实现interface

的类的对象

在示例ArrayListLinkedList中,两者都实现了List接口,因此可以传递给该方法。最终,您的方法获得的是实现List接口的类的对象。