且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以在没有实现接口的情况下调用接口内部的方法吗?

更新时间:2022-12-04 18:06:07

静态地,你可以声明它被调用。您不一定要在调用类中实现内部接口。例如:

Statically, you can declare it to be called. You don't necessarily have to implement the interface inside the calling class. For example:

public class HulkHogan { 
  private IPerson person;

  public HulkHogan(IPerson person){
    this.person = person;
  }

  public void doSomething(Contant contact){
    //call your method here
    person.savePerson(contact);
  }
}

但是为了执行它,你需要在某个地方实现它,因为接口只描述某些行为的行为但不提供实际行为(即不实现它)。

But in order to execute it, you will need to implement it somewhere, because interface just describes how something behaves but does not provide the actual behaviour (i.e. does not implement it).