1

我的代码中出现了这个编译错误:

方法 add(UComponent comp, KopiAlignment constraints); 对于 DBlock 类型是模棱两可的...

我想创建 UBlock 接口以对现有类 DBlock 进行抽象

这是代码:

界面 UBlock :

public interface UBlock extends UComponent, BlockListener {
   public void add(UComponent comp, KopiAlignment constraints);
}

DBlock 类:

public class DBlock extends JPanel implements  UBlock {
    public void add(UComponent comp, KopiAlignment constraints) {
    } 
}

我在另一个类中调用 add 方法:

private DBlock blockView;

blockView.add(displays[i], new KopiAlignment(chartPos + leftOffset, i + 1, 1, false));

当我从 DBlock 中删除 UBlock 的实现时,错误不再存在,这是被调用的方法:

/**
 * Adds the specified component to the end of this container.
 * Also notifies the layout manager to add the component to 
 * this container's layout using the specified constraints object.
 * This is a convenience method for {@link #addImpl}.
 * <p>
 * Note: If a component has been added to a container that
 * has been displayed, <code>validate</code> must be
 * called on that container to display the new component.
 * If multiple components are being added, you can improve
 * efficiency by calling <code>validate</code> only once,
 * after all the components have been added.
 *
 * @param     comp the component to be added
 * @param     constraints an object expressing 
 *                  layout contraints for this component
 * @exception NullPointerException if {@code comp} is {@code null}
 * @see #addImpl
 * @see #validate
 * @see javax.swing.JComponent#revalidate()
 * @see       LayoutManager
 * @since     JDK1.1
 */
public void add(Component comp, Object constraints) {
    addImpl(comp, constraints, -1);
}

那么我该如何解决这个问题呢?

4

1 回答 1

1

问题是您继承了另add一种方法。这意味着在你的DBlock课堂上你有两个

public void add(UComponent comp, KopiAlignment constraints)

public void add(Component comp, Object constraints)

由你处置。

现在KopiAlignment是 的子类型,Object我猜UComponent是 的子类型Component。这意味着当您add使用UComponentKopiAlignment作为参数调用时,参数类型适合这两种方法,尽管它们在形式上具有不同的签名。

据我所知,没有真正的方法可以解决这个问题。

于 2013-09-27T14:11:32.133 回答