我的代码中出现了这个编译错误:
方法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);
}
那我怎么解决这个问题呢?
答案 0 :(得分:1)
问题是您继承了另一个add
方法。这意味着在DBlock
课程中您同时拥有
public void add(UComponent comp, KopiAlignment constraints)
和
public void add(Component comp, Object constraints)
随时为您服务。
现在KopiAlignment
是Object
的子类型,我猜UComponent
是Component
的子类型。这意味着当您使用add
和UComponent
作为参数调用KopiAlignment
时,参数类型适合这两种方法,尽管它们正式具有不同的签名。
据我所知,没有真正的方法可以解决这个问题。