我可以在没有实现接口的情况下调用接口内部的方法吗? [英] Can i call a method which is inside an interface without implementing the interface?

查看:468
本文介绍了我可以在没有实现接口的情况下调用接口内部的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以调用接口内的方法而不在我的类中实现接口吗?

Can i call a method inside an interface without implementing the interface in my class?

package;

import Contact;

public interface IPerson{

    public void savePerson(Contact contact);

}

现在这里的一些课......

Now some class here...

public class HulkHogan { 

//Calling the method savePerson here
//I dont want to implement the Interface in all.

}


推荐答案

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

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).

这篇关于我可以在没有实现接口的情况下调用接口内部的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆