2

首先,这不是这个问题的重复,即使它们具有相同的标题。这个问题是指传递与 C 函数基本相同的方法:它们不需要属于特定对象。在这种情况下,您可以传递一个RunnableCallable对象。

相反,我要问的是:是否可以传递对类中特定方法的引用,并为特定对象调用该方法?

例如,我查看了FlowLayoutSwing 中的代码,发现preferredLayoutSizeminimumLayoutSize实现完全相同,除了一行:

public Dimension preferredLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    Dimension dim = new Dimension(0, 0);
    int nmembers = target.getComponentCount();
    boolean firstVisibleComponent = true;
    boolean useBaseline = getAlignOnBaseline();
    int maxAscent = 0;
    int maxDescent = 0;

    for (int i = 0 ; i < nmembers ; i++) {
        Component m = target.getComponent(i);
        if (m.isVisible()) {
            Dimension d = m.getPreferredSize();
            dim.height = Math.max(dim.height, d.height);
            if (firstVisibleComponent) {
                firstVisibleComponent = false;
            } else {
                dim.width += hgap;
            }
            dim.width += d.width;
            if (useBaseline) {
                int baseline = m.getBaseline(d.width, d.height);
                if (baseline >= 0) {
                    maxAscent = Math.max(maxAscent, baseline);
                    maxDescent = Math.max(maxDescent, d.height - baseline);
                }
            }
        }
    }
    if (useBaseline) {
        dim.height = Math.max(maxAscent + maxDescent, dim.height);
    }
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right + hgap*2;
    dim.height += insets.top + insets.bottom + vgap*2;
    return dim;
  }
}

public Dimension minimumLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    boolean useBaseline = getAlignOnBaseline();
    Dimension dim = new Dimension(0, 0);
    int nmembers = target.getComponentCount();
    int maxAscent = 0;
    int maxDescent = 0;
    boolean firstVisibleComponent = true;

    for (int i = 0 ; i < nmembers ; i++) {
        Component m = target.getComponent(i);
        if (m.visible) {
            Dimension d = m.getMinimumSize();
            dim.height = Math.max(dim.height, d.height);
            if (firstVisibleComponent) {
                firstVisibleComponent = false;
            } else {
                dim.width += hgap;
            }
            dim.width += d.width;
            if (useBaseline) {
                int baseline = m.getBaseline(d.width, d.height);
                if (baseline >= 0) {
                    maxAscent = Math.max(maxAscent, baseline);
                    maxDescent = Math.max(maxDescent,
                                          dim.height - baseline);
                }
            }
        }
    }

    if (useBaseline) {
        dim.height = Math.max(maxAscent + maxDescent, dim.height);
    }

    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right + hgap*2;
    dim.height += insets.top + insets.bottom + vgap*2;
    return dim;
  }
}

该方法preferredLayoutSize调用(第-个组件)的方法,而调用 的方法。据我所知,这两种方法在其他方面是相同的。 preferredLayoutSizemiminimumLayoutSizeminimumLayoutSizem

正如任何程序员都会告诉你的那样,代码重复是一件坏事。然而,在这种情况下,如何摆脱重复代码并不明显。很明显,应该有一个私有方法,其中包含两个公共方法调用的代码,传入对类的preferredLayoutSizeminimumLayoutSize方法的引用Component。在 CI 中可以使用函数指针来做到这一点,因此在 Java 中应该有某种方式来做到这一点是有道理的。传入 aRunnableCallable几乎可以工作,但都不返回值。 编辑:这是错误的。a 中被覆盖的方法Callable 确实返回一个值,并且它所作用的对象可以作为参数传入。

既然我已经输入了所有内容,我想到了一个解决方案:您可以编写一个interface带有调用的方法Dimension layoutSize(Component comp)并编写两个实现,一个返回comp.preferredLayoutSize(),另一个返回comp.minimumLayoutSize()。然后,您可以编写一个将该接口的实例作为参数的私有方法,并使用它在代码中的正确位置运行单独的方法。您甚至可以使用匿名内部类,因此您不必为每种布局大小类型编写新类。不过,对于一个相当简单的问题来说,这似乎仍然很麻烦。有没有更简单的方法?

4

3 回答 3

2

您可以将其中一个方法包装在一个类LayoutSizing(可能是一个本地类)中,使用一个抽象方法来计算 on m,使用mas 参数,然后在这两个方法中实例化new LayoutSizing() { @Override ... }并实现该方法。

在 Java 8 中,这将有望看起来更好看。

于 2013-09-18T14:50:36.813 回答
2

你可以使用一个接口:

interface DimensionReturningThingy {
    public Dimension getDim (Component c);
}

public Dimension preferredLayoutSize(Container target) {
    commonCode (target, new DimensionReturningThingy () {
        public Dimension getDim (Component m) {
            return m.getPreferredSize ();
        });
}

// and similarly for minimumLayoutSize

public Dimension commonCode (Container target, DimensionReturningThingy drt) {

// now repeat code above, except that replace

        Dimension d = m.getPreferredSize();

// with

        Dimension d = drt.getDim (m);

我不擅长命名事物,所以我相信你可以为其中的一些想出更好的名字。

编辑:在您编辑原始帖子以提及界面解决方案之前,我想我正在努力回答这个问题。

于 2013-09-18T14:55:07.253 回答
1

对于这种特殊情况,最简单的解决方案是将这两种方法重构为三个:

private Dimension layoutSize(Container target, boolean prederred) {
    ...
    Dimension d = prederred?m.getPreferredSize():m.minimumLayoutSize();
    ...
}

public Dimension preferredLayoutSize(Container target) {
    return layoutSize(target, true);
}

public Dimension minimumLayoutSize(Container target) {
    return layoutSize(target, false);
}
于 2013-09-18T15:44:05.940 回答