【问题标题】:abstract class multiple inheritance抽象类多重继承
【发布时间】:2016-07-06 11:16:31
【问题描述】:

我有以下类结构:

  • 抽象类A
  • B 类扩展 A 类
  • 抽象类 C 扩展类 B
  • D 类扩展 C 类

我有一个静态方法,它的返回类型为 A 并返回 B - 这没关系。 我想更改这个方法,所以它仍然有返回类型A,但它返回D - 这会导致问题:

java.lang.Error: Unresolved compilation problem: 
    D cannot be resolved to a type

为什么会这样?它们应该具有与父类相同的接口,不是吗? 编辑:

这是我试图修改的方法

public static CodeFormatter createDefaultCodeFormatter(Map<String, ?> options) {
        if (options == null)
            options = CCorePlugin.getOptions();
        return new CCodeFormatter(options);
    }

它是来自 CDT 项目 (http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=a83be9db5b41c0a593425798a2af91060588b38a) 的一个类。我试图返回 MyFormatter 而不是 CCodeFormatter。 CCodeFormatter 类(示例中的 B)扩展了 CodeFormatter(示例中的 A)并由抽象类 MyFormatter(或示例中的 C)扩展并且此类由 MyFormatter 类扩展(示例中为 D)。因此,我将其项目添加到所需的捆绑包中(在 MANIFEST.MF 中),然后将其导入并返回新的 MyFormatter(选项)。最后出现这个错误。

public static CodeFormatter createDefaultCodeFormatter(Map<String, ?> options) {
        if (options == null)
            options = CCorePlugin.getOptions();
        return new MyFormatter(options);
    }

“D”类

public class MyFormatter extends MyAbstractFormatter{

    @Override
    protected CodeFormatterVisitor getFormatterVisitor(DefaultCodeFormatterOptions preferences, int offset, int length) {
        return new MyFormatterVisitor(preferences, offset, length);
    }

}

和“C”类

public abstract class MyAbstractFormatter extends CCodeFormatter {

    public MyAbstractFormatter() {
        super();
    }

    public MyAbstractFormatter(DefaultCodeFormatterOptions preferences) {
        super(preferences);
    }

    public MyAbstractFormatter(DefaultCodeFormatterOptions defaultCodeFormatterOptions, Map<String, ?> options) {
        super(defaultCodeFormatterOptions, options);
    }

    public MyAbstractFormatter(Map<String, ?> options) {
        super(options);
    }

    @Override
    public void setOptions(Map<String, ?> options) {
        super.setOptions(options);
    }

    @Override
    public TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) {
        return super.format(kind, source, offset, length, indentationLevel, lineSeparator);
    }

}

【问题讨论】:

  • 你导入D了吗? (请出示代码,否则很不幸,如果有其他问题,您可以得到帮助)
  • 与继承无关。它只是说D 在您尝试使用它时不可见。
  • 当然我已经导入了 D。Eclipse 甚至提供我更改方法类型Type mismatch: cannot convert from D to A. +Change method return type to D。也许Eclipse项目中的bundle import有问题,我会调查一下。
  • 你需要发布一些代码,否则只是猜测......
  • 显示您的代码,否则我们无法提供您需要的确切答案

标签: java oop inheritance


【解决方案1】:

好的,问题似乎在于循环依赖(而不是抽象类)。我的项目依赖于 CDT 项目,我正在尝试将我的项目添加到该项目所需的捆绑包中,我正在使这个 CDT 项目依赖于我的项目。 (Altought Eclipse 通常会检测到这个问题,可能不是这样)。

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多