接口与实现类有什么样的关系?

1 java

子类具有IS-A与其基类一起描述的关系,但是基类不与其子类共享这种关系。我一直在徘徊接口与实现类之间的关系,因为该类的对象可以传递给接口对象,并且接口对象只能访问为它定义的具体接口的方法。

public class main {
    public static void main(String[]args){

        Nigeria ng = new Nigeria(){};

        //Interface object can accept Nigerias object which is not posible in Inheritance
        Continent continent = ng; 

        //prints Country is in Africa
        continent.Africa(); 

        //continent.language(); will not compile language is not in the interface

        //Print Democratic thought this should print Undefined since it is inialied with default.
        continent.Goverment(); 

    }

}
interface Continent{
    public void Africa();
    default void Goverment(){
        System.out.println("Undefined");
    }
}
class Nigeria implements Continent{
    @Override
    public void Africa(){
        System.out.println("Country is in Africa");
    }   
    public void language(){
        System.out.println("Official Language is English");
    }
    public void Goverment(){
        System.out.println("Democratic");
    }
}
Run Code Online (Sandbox Code Playgroud)

Nor*_*ard 5

如果您正在寻找英语类似物,则接口不是“是...”或“是...”的关系,而是一个“是...”的关系。

接口与使用它的类无关。
它是关于消费者的要求。

如果您希望将其视为任何事物,则可以将其视为形容词。

“他是负责任的”。

好吧,他做什么?

他完成任务;他对自己的错误负责;他说对了。

他是飞行员,还是外科医生,还是医生?
他是孩子,父亲还是曾祖父?

你关心?

我需要一个负责任的人来帮助我完成这项工作。
ResponsiblePerson是否从PoliceOfficer继承?律师是否继承自ResponsiblePerson,因为我敢肯定会有不负责任的律师。

class Lawyer extends Person { }
class ResponsibleLawyer extends Lawyer implements ResponsibleEntity { }


class NeedyPerson extends Person {
  public void acceptHelp (ResponsibleEntity somebody) {
    try {
      somebody.attemptTask( someTask );
    } catch (TaskCompletionError err) {
      somebody.takeOwnership(err);
      somebody.fixMistake(err);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

公司也可以负责吗?
也许我们不经常看到它,但是从理论上讲这是可能的:

class LawFirm extends CorporateEntity { }
class BetterLawFirm extends LawFirm implements ResponsibleEntity { }
Run Code Online (Sandbox Code Playgroud)

可以somebody成为负责任的法人团体吗?好吧,只要该法人团体确实可以执行与负责人本来应该做的所有相同的事情。

在另一个示例中,您可能有一个Switchable接口。
看这个名字,您可能会猜到,所给的东西有一个可以拨动的开关。
那么它可能有什么方法呢?

on( )
off( )
toggle( )
isOn( )

听起来好像很有用。

这样的界面有什么好处?
好吧,现在我知道我可以处理一个开关了,它的血统也没关系。

如果我想要的只是一个需要切换并对其进行处理的类,那么为什么我需要创建几十个类,以仅接受带有切换的我的许多东西?
或将方法覆盖到污垢中以执行相同操作。

class SwitchThrower {
  public void throwSwitch (CoffeeMaker coffeeMaker) { coffeeMaker.on(); }
  public void throwSwitch (LightSwitch lightSwitch) { lightSwitch.on(); }
  public void throwSwitch (GhostTrap ghostTrap) { ghostTrap.on(); }
  public void throwSwitch (TheHeat theHeat) { theHeat.on(); }
  public void throwSwitch (CarIgnition ignition) { ignition.on(); }
}
Run Code Online (Sandbox Code Playgroud)

...

为什么不只是:

class SwitchThrower {
  public void throwSwitch (Switchable switch) { switch.on(); }
}


class LightSwitch implements Switchable {
  private boolean currentlyOn;

  public LightSwitch (boolean initiallyOn) {
    currentlyOn = initiallyOn;
  }

  public LightSwitch () {
    currentlyOn = false;
  }

  public boolean on () {
    currentlyOn = true;
    return currentlyOn;
  }

  public boolean off () {
    currentlyOn = false;
    return currentlyOn;
  }

  public boolean toggle (boolean forceOn) {
    boolean state;
    if (forceOn == true) {
      state = on();
    } else {
      state = off();
    }
    return state;
  }

  public boolean toggle () {
    boolean state;
    if (isOn() == true) {
      state = off();
    } else {
      state = on();
    }
    return state;
  }

  public boolean isOn () {
    return currentlyOn;
  }
}
Run Code Online (Sandbox Code Playgroud)

...等等

如您所见,除了描述实现程序的基本功能集之外,接口根本不是关于类的,而是关于使用者的

_Traits_使用不同语言的更出色的实现
特性通常类似于接口,但是它们具有与之关联的默认行为。

看看my Switchable和my LightSwitch,您可以想象几乎所有带有此开关的类都将具有相同的方法,具有相同的方法行为...

...所以,如果我已经在接口中定义签名的麻烦了,为什么还要重新重写所有这些方法呢?
为什么我不能只在其中添加默认行为,并将其应用于实现者,除非方法被重写?

好吧,这就是特质/混搭所允许的。


ΦXo*_*a ツ 2

这种关系只是类实现接口提供的方法的“契约”。

这就是java如何分离对象可以做什么(接口)和继承的类将如何做。