Java通用参数扩展了A或B. [英] Java Generic argument extends A or B

查看:92
本文介绍了Java通用参数扩展了A或B.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class MyClass< T extends Number> 

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

  class MyClass< T extends Number&可比> 

我的问题是;

 类MyClass< T扩展JComponent或JFrame>如何指定泛型参数必须扩展两个类中的一个? 

我有一个 Propery 类,在这个时候它看起来像这样。

  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();
}
};
...
}

尽管更多的属性像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);
尝试{
Thread.sleep((long)timeout);
} catch(InterruptedException e){
e.printStackTrace();



$ b public static void animateProperty(final JComponent component,final Property< Color> property,Color to,double duration,double fps){
final颜色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);
}
},持续时间,fps);
}

public static void animateProperty(final JFrame component,final Property< Color> property,Color to,double duration,double fps){
final color currentValue = property.get (零件);
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);
}
},持续时间,fps);
}
}

我删除了一些方法,但您应该得到IDE。
我必须为每个单一方法编写两次,一次是为JFrame编写,一次是为JComponent编写,这是一个很痛苦的过程。

解决方案

我不认为这是可能的,那么构建这个构造是没有意义的。



当您使用A& B,那么你知道你将能够在你有的实例上调用A和B的方法,但是如果你能够指定A或B,那么编译器是否可以调用Aa()或者你可以通过cal Bb();

如果你需要这样的构造,你可能会遇到问题,发布你的代码,我们可能能够看看你的代码,也许你应该围绕JFrame和JComponent使用一个适配器来隐藏重复的代码,尽管JFrame和JComponent都是容器你不能使用它吗


In Java, we have generics and Generic arguments may be specified to have to extends/implement a certain class/interface.

class MyClass<T extends Number>

One can also specify that it has to implement several interfaces

class MyClass<T extends Number & Comparable>

My question is; how can I specify that a generic argument must extend one of two classes?

Class MyClass<T extends JComponent OR JFrame>

What I have is a Propery class, which at this time look like this.

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();
        }
    };
   ...
}

Although, more properties, like BACKGROUND_COLOR, and LOCATION.

The problem really starts here, as you may be able to se the duplicate code for every property, but it gets worse in the Animation class:

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);
    }
}

I stripped out some methods, but you should get the idé. I have to write every single method twice, once for JFrame and once for JComponent, which is a pain.

解决方案

I don't think that's possible, well it doesn't make sense to have that construct.

When you specify your parameters using A & B then you know that you will be able to call the methods of A and B on the instance that you have, but if you would be able to specify A OR B then it would be ambiguous to the compiler whether you can call A.a() or you can cal B.b();

If you have the need for such a construct you're likely to have deisng issues, post your code and we might be able to help

Seeing your code, maybe you should use an Adapter around the JFrame and JComponent that will hide the duplicated code, though JFrame and JComponent are both Container can you not use that

这篇关于Java通用参数扩展了A或B.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆