当有现有类时,接口如何取代多重继承的需要 [英] How can interfaces replace the need for multiple inheritance when have existing classes

查看:100
本文介绍了当有现有类时,接口如何取代多重继承的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先......对不起这篇文章。我知道stackoverflow上有很多帖子正在讨论多重继承。但我已经知道Java不支持多重继承,我知道使用接口应该是另一种选择。但我不明白并且看到了我的困境:

First of all... Sorry for this post. I know that there are many many posts on stackoverflow which are discussing multiple inheritance. But I already know that Java does not support multiple inheritance and I know that using interfaces should be an alternative. But I don't get it and see my dilemma:

我必须对用Java编写的非常大而复杂的工具进行更改。在此工具中,有一个数据结构,它由许多不同的类对象构建,并具有链接的成员层次结构。无论如何......

I have to make changes on a very very large and complex tool written in Java. In this tool there is a data structure built with many different class objects with a linked member hierarchy. Anyway...


  • 我有一个类标记,它有多个方法并返回一个对象标签,取决于对象的类。它需要成员和静态变量。

  • 第二个名为 XMLElement 的类允许链接对象,最后生成XML文件。我还需要成员和静态变量。

  • 最后,我有这么多的数据类几乎都应该扩展 XMLElement 和一些他们标记

  • I have one class Tagged which has multiple methods and returns an object tag depending on the object's class. It needs members and static variables.
  • And a second class called XMLElement allows to link objects and in the end generate a XML file. I also need member and static variables here.
  • Finally, I have these many many data classes which nearly all should extend XMLElement and some of them Tagged.

好的,这不行,因为它是只能扩展一个类。我经常读到Java的所有内容都没问题,也没有必要进行多重继承。我相信,但我不知道接口应如何取代继承。

Ok ok, this won't work since it's only possible to extend just one class. I read very often that everything with Java is ok and there is no need for having multiple inheritance. I believe, but I don't see how an interface should replace inheritance.


  1. 将实际实现放在所有数据类中是没有意义的,因为它每次都是相同的但是这对接口来说是必要的(我认为)。

  2. 我看不出如何将一个继承类更改为接口。我在这里有变量,它们必须完全在那里。

我真的不明白所以请有人解释我怎么样处理这个?

I really don't get it so please can somebody explain me how to handle this?

推荐答案

你可能更喜欢组合(和委托)而不是继承:

You should probably favor composition (and delegation) over inheritance :

public interface TaggedInterface {
    void foo();
}

public interface XMLElementInterface {
    void bar();
}

public class Tagged implements TaggedInterface {
    // ...
}

public class XMLElement implements XMLElementInterface {
    // ...
}

public class TaggedXmlElement implements TaggedInterface, XMLElementInterface {
    private TaggedInterface tagged;
    private XMLElementInterface xmlElement;

    public TaggedXmlElement(TaggedInterface tagged, XMLElementInterface xmlElement) {
        this.tagged = tagged;
        this.xmlElement = xmlElement;
    }

    public void foo() {
        this.tagged.foo();
    }

    public void bar() {
        this.xmlElement.bar();
    }

    public static void main(String[] args) {
        TaggedXmlElement t = new TaggedXmlElement(new Tagged(), new XMLElement());
        t.foo();
        t.bar();
    }
}

这篇关于当有现有类时,接口如何取代多重继承的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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