Java泛型参数扩展了A或B

Java泛型参数扩展了A或B,java,generics,Java,Generics,在Java中,我们有泛型,泛型参数可能被指定为必须扩展/实现某个类/接口 class MyClass<T extends Number> 不过,还有更多属性,如背景颜色和位置 问题实际上从这里开始,因为您可能可以为每个属性使用重复代码,但在动画类中情况会更糟: public class Animation { public static void callbackAnimation(AnimationCallback callback, double duration,

在Java中,我们有泛型,泛型参数可能被指定为必须扩展/实现某个类/接口

class MyClass<T extends Number>
不过,还有更多属性,如背景颜色和位置

问题实际上从这里开始,因为您可能可以为每个属性使用重复代码,但在动画类中情况会更糟:

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<Color> 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<Color> 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);
    }
}
公共类动画{
公共静态void callbackAnimation(AnimationCallback回调,双持续时间,双fps){
双超时=(1/fps)*1000;
int迭代次数=(int)(持续时间/超时);

对于(int i=0;i这是不可能的;如果您指定这两个类的公共基类,则只能隐式地执行此操作。泛型用于了解其中的内容。这并不意味着“一个或另一个”。

我认为这是不可能的,具有此结构是没有意义的

当您使用A&B指定参数时,您知道您将能够在您拥有的实例上调用A和B的方法,但是如果您能够指定A或B,那么编译器将无法确定您是否可以调用A.A()或调用B.B()

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

看到您的代码,也许您应该在JFrame和JComponent周围使用一个适配器,它将隐藏重复的代码,尽管JFrame和JComponent都是,但您不能使用它吗

Class MyClass<T extends JComponent OR JFrame>
public abstract class Property<T> {

    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<Integer> HEIGHT = new Property<Integer>() {
        @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<Integer> WIDTH = new Property<Integer>() {
        @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();
        }
    };
   ...
}
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<Color> 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<Color> 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);
    }
}