gpt4 book ai didi

java - 抽象类多重继承

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:22 25 4
gpt4 key购买 nike

我有以下类结构:

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

我有一个静态方法,其返回类型为A并返回B - 这是可以的。我想更改此方法,因此它仍然具有返回类型A,但它返回D - 这会导致问题:

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

为什么会发生这种情况?它们应该与父类具有相同的接口(interface),不是吗?编辑:

这是我尝试修改的方法

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(options)。最终还是出现了这个错误。

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);
}

}

最佳答案

好吧,问题似乎出在循环依赖上(而不是在抽象类中)。我的项目依赖于 CDT 项目,并且我正在尝试将我的项目添加到该项目所需的包中,我正在使该 CDT 项目依赖于我的项目。 (尽管 Eclipse 通常会检测到此问题,但情况可能并非如此)。

关于java - 抽象类多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com