簡體   English   中英

方法“ XXX”對於類型不明確

[英]The method “XXX” is ambigious for the type

我的代碼中出現此編譯錯誤:

方法add(UComponent comp,KopiAlignment約束); 對於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);
}

那么我該如何解決這個問題呢?

問題是您繼承了另一個add方法。 這意味着在DBlock類中,

public void add(UComponent comp, KopiAlignment constraints)

public void add(Component comp, Object constraints)

由你處置。

現在KopiAlignmentObject的子類型,我猜UComponentComponent的子類型。 這意味着當您調用以UComponentKopiAlignment作為參數的add時,參數類型適合這兩種方法,盡管它們在形式上具有不同的簽名。

據我所知,沒有真正的方法可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM