简体   繁体   English

了解多重继承和接口

[英]To understand Multiple inheritance and interfaces

I understand multiple inheritance and interface to an extent. 我在某种程度上了解多重继承和接口。 Is it possible to use Multiple inheritance to achieve my requirement? 是否可以使用多重继承来达到我的要求?

I have classes A,B,C,D which implements interface InterA, InterB, InterC, InterD respectively. 我有A,B,C,D类,分别实现接口InterA,InterB,InterC,InterD。 I want a class ABCD which should have all the methods of A, B, C, D. Then I want the declared methods in InterA, InterB, InterC, InterD available at class ABCD. 我想要一个ABCD类,它应该具有A,B,C,D的所有方法。然后,我希望在ABA类中可以使用InterA,InterB,InterC,InterD中声明的方法。

I have implementations of methods in InterA, InterB, InterC, InterD already defined in classes A,B,C,D, which I don't want to define again in class ABCD. 我已经在类A,B,C,D中定义了InterA,InterB,InterC,InterD中的方法的实现,而我不想在ABCD类中再次定义。 How can I do this in Java? 如何用Java做到这一点?

The purpose of an interface is to ensure that a class that implements that interface will implement all the methods declared in that interface. 接口的目的是确保实现该接口的类将实现该接口中声明的所有方法。

Eg: 例如:

public interface Flyer {

    public void fly();
}

The above interface ensures that any Java class that implements the Flyer interface will have an implementation for the fly method, irregardless of how the flight is implemented. 上面的接口可确保任何实现Flyer接口的Java类都将具有fly方法的实现,而不管Flight是如何实现的。

Eg: 例如:

public class Bird implements Flyer {

    public void fly() {
        System.out.println("Bird is flapping");
    }
}

And another eg: 另一个例子:

public class Eagle implements Flyer {

    public void fly() {
        System.out.println("Eagle is soaring");
    }
}

With an interface, I am assured that the fly() method will exist despite different implementations. 通过一个接口,我确信尽管实现方式不同,fly()方法仍将存在。

So if you want a class ABCD that has the behavior of 4 different interfaces, as specified in interface A, B, C and D, then you can implement: 因此,如果您想要一个具有4个不同接口(如接口A,B,C和D中指定)的行为的ABCD类,则可以实现:

public class ABCD implements A,B,C,D {

    // methods here
}

Usually, we use interfaces to inherit behavior, and parent/child classes to inherit a model. 通常,我们使用接口来继承行为,并使用父/子类来继承模型。

How do we use the Flyer interface then? 那么我们如何使用Flyer界面呢?

Sample code: 样例代码:

public class FlyerTest {

    public static void main (String [] args) {
        Flyer flyer = new Eagle();
        flyer.fly();    // Prints out "Eagle is soaring"

        flyer = new Bird();
        flyer.fly();    // Prints out "Bird is flapping"
    }

}

As you see, as long as I have a Flyer, I am assured that the "fly" method will have an implementation. 如您所见,只要我有一个Flyer,就可以确信“ fly”方法将具有实现。 This takes away the need for me to wonder if the method actually exists. 这使我无需怀疑该方法是否确实存在。

This is why we use interfaces to inherit behavior. 这就是为什么我们使用接口来继承行为。

Hope you have a better understanding of multiple inheritances. 希望您对多重继承有更好的了解。

To answer your core question, Object-Oriented programming is the one that helps to promote code re-use in general. 为了回答您的核心问题,面向对象编程是一种有助于总体上促进代码重用的编程。

Since there is no multiple inheritance in Java you have to resort to aggregation : 由于Java中没有多重继承,因此您必须求助于聚合

class ABCD implements InterA, InterB, InterC, InterD
{
   A implA;
   B implB;
   C implC;
   D implD;

   ABCD(A pimplA, B pimplB, C pimplC, D pimplD)
   {
     implA = pimplA;
     implB = pimplB;
     implC = pimplC;
     implD = pimplD;
  }


  // @overidde methods from InterA as return implA ->method();
  // @overidde methods from InterB as return implB ->method();
  // @overidde methods from InterC as return implC ->method();
  // @overidde methods from InterD as return implD ->method();
}

That way you just have to create some instances of A, B, C and D and pass them at the construction of ABCD. 这样,您只需要创建A,B,C和D的一些实例,然后将它们传递给ABCD。 Then ABCD just have to call their methods . 然后, ABCD只需调用其方法即可

By doing that you will reuse the implementations of A, B, C and D . 这样,您将重用A,B,C和D的实现

there is no such a thing as "real" multiple inheritance in java. java中没有“真正的”多重继承之类的东西。 You cannot do sth. 你不能做某事。 like public class D extends C,B,A {} All you can do is either implement multiple interfaces or you can make B extend A, C extend B and D extend C public class D extends C,B,A {}所能做的就是要么实现多个接口,要么可以使B扩展A,C扩展B和D扩展C

There is no multiple inheritance in java. Java中没有多重继承。 Why don't you just create objects of those classes in ABCD and use them to access the methods of those classes? 为什么不只在ABCD中创建这些类的对象并使用它们来访问那些类的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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