Java Generic参数扩展A或B

在Java中,我们有generics,并且可以指定通用参数来扩展/实现某个类/接口。

class MyClass 

还可以指定它必须实现多个接口

 class MyClass 

我的问题是; 如何指定generics参数必须扩展两个类中的一个?

 Class MyClass 

我所拥有的是一个Propery课程,此时此类似乎。

 public abstract class Property { public abstract void set(JComponent component, T value); public abstract void set(JFrame component, T value); public abstract T get(JComponent component); public abstract T get(JFrame component); public static Property HEIGHT = new Property() { @Override public void set(JComponent component, Integer value) { component.setSize(component.getWidth(), value); } @Override public Integer get(JComponent component) { return component.getHeight(); } @Override public void set(JFrame component, Integer value) { component.setSize(component.getWidth(), value); } @Override public Integer get(JFrame component) { return component.getHeight(); } }; public static Property WIDTH = new Property() { @Override public void set(JComponent component, Integer value) { component.setSize(value, component.getHeight()); } @Override public Integer get(JComponent component) { return component.getWidth(); } @Override public void set(JFrame component, Integer value) { component.setSize(value, component.getHeight()); } @Override public Integer get(JFrame component) { return component.getWidth(); } }; ... } 

虽然有更多属性,例如BACKGROUND_COLOR和LOCATION。

问题确实从这里开始,因为你可以为每个属性设置重复的代码,但在Animation类中它会变得更糟:

 public class Animation { public static void callbackAnimation(AnimationCallback callback, double duration, double fps) { double timeout = (1/fps)*1000; int iterations = (int)(duration/timeout); for (int i = 0; i <= iterations; i++) { callback.run((double)i/(double)iterations); try { Thread.sleep((long)timeout); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void animateProperty(final JComponent component, final Property property, Color to, double duration, double fps) { final Color currentValue = property.get(component); final int differenceRed = to.getRed() - currentValue.getRed(); final int differenceGreen = to.getGreen() - currentValue.getGreen(); final int differenceBlue = to.getBlue() - currentValue.getBlue(); final int differenceAlpha = to.getAlpha() - currentValue.getAlpha(); Animation.callbackAnimation(new AnimationCallback() { @Override public void run(double fraction) { Color newColor = new Color( (int)(currentValue.getRed()+(differenceRed*fraction)), (int)(currentValue.getGreen()+(differenceGreen*fraction)), (int)(currentValue.getBlue()+(differenceBlue*fraction)), (int)(currentValue.getAlpha()+(differenceAlpha*fraction)) ); property.set(component, newColor); } }, duration, fps); } public static void animateProperty(final JFrame component, final Property property, Color to, double duration, double fps) { final Color currentValue = property.get(component); final int differenceRed = to.getRed() - currentValue.getRed(); final int differenceGreen = to.getGreen() - currentValue.getGreen(); final int differenceBlue = to.getBlue() - currentValue.getBlue(); final int differenceAlpha = to.getAlpha() - currentValue.getAlpha(); Animation.callbackAnimation(new AnimationCallback() { @Override public void run(double fraction) { Color newColor = new Color( (int)(currentValue.getRed()+(differenceRed*fraction)), (int)(currentValue.getGreen()+(differenceGreen*fraction)), (int)(currentValue.getBlue()+(differenceBlue*fraction)), (int)(currentValue.getAlpha()+(differenceAlpha*fraction)) ); property.set(component, newColor); } }, duration, fps); } } 

我删除了一些方法,但你应该得到idé。 我必须两次编写每个方法,一次用于JFrame,一次用于JComponent,这很痛苦。

我不认为这是可能的,拥有那种结构是没有意义的。

当您使用A和B指定参数时,您就知道您将能够在您拥有的实例上调用A和B的方法,但是如果您能够指定A OR B那么它将是不明确的编译器是否可以调用Aa()或者你可以调用Bb();

如果您需要这样的构造,您可能会遇到deisng问题,发布您的代码,我们可能会提供帮助

看到你的代码,也许你应该在JFrame和JComponent周围使用一个可以隐藏重复代码的适配器,虽然JFrame和JComponent都是容器你不能使用它

这是不可能的; 如果指定这两个类的公共基类,则只能隐式执行。 generics用于了解内部的内容。 这并不意味着“一个或另一个”。