简体   繁体   English

Java泛型问题

[英]Trouble with Java generics

I'm trying to do something with generics and I can't seem to figure it out. 我正在尝试用泛型做一些事情,我似乎无法弄明白。 So I have this interface. 所以我有这个界面。

public interface Transformation<A,B> {
    public B transform(A a);
}

And I have a class that implements it. 我有一个实现它的类。

public class AveragingTransformer implements Transformation<List<Long>,Double> {

    public Double transform(List<Long> a) {
        return a.get(0).doubleValue()/a.get(1);
    }

}

So in my main method I have this... 所以在我的主要方法中我有这个......

public static void main(String[] args){
    ....
    Transformation<List<?>,Double> transformation = new AveraginTransformer();
    ....
}

But this doesn't work for some reason, can anyone help me understand why? 但是由于某些原因这不起作用,任何人都可以帮助我理解为什么吗?

EDIT 编辑

So the issue is if I want to pass an array to my transformation I can't seem to get the compiler to accept it. 所以问题是如果我想将数组传递给我的转换,我似乎无法让编译器接受它。

So say I have... 所以说我有......

public class foo<T>{

    T val;
    Transformation<? extends List<?>,T> transformation; //This gets initialized by constructor, where I will pass in the averaging transformer in this case.

    public void setVal(List<?> list){
        val = transformation.transform(list);
    }

}

If I try to do this it gives me this error 如果我尝试这样做,它会给我这个错误

在此输入图像描述

But I want to keep the left part of my transformation generic because different foos will calculate their values in different ways with potentially different data. 但是我想保持转换的左侧部分是通用的,因为不同的foos将以不同的方式使用可能不同的数据来计算它们的值。 ie Some foos might calculate their val from a list of Strings but if I provide the right transformer to each, then it should work. 即一些foos可能会从字符串列表中计算出它们的val,但如果我为每个字符串提供正确的变换器,那么它应该可以工作。

You must match the generics exactly: 您必须完全匹配泛型:

    Transformation<List<Long>,Double> transformation  = new AveragingTransformer();

ie change List<?> to List<Long> . 即将List<?>更改为List<Long>

If you want the tool to apply to multiple types then choose an appropriate implemented interface. 如果您希望该工具应用于多种类型,请选择适当的已实现接口。 eg here Long implements Number so I will pull back to List<Number> . 例如,这里Long实现Number所以我将回到List<Number>

public interface Transformation<A, B> {

    public B transform(A a);
}

public class AveragingTransformer implements Transformation<List<Number>, Double> {

    @Override
    public Double transform(List<Number> a) {
        return a.stream().mapToDouble(v -> v.doubleValue()).average().orElse(0);
    }

}

public void test() {
    Transformation<List<Number>, Double> transformation = new AveragingTransformer();
}

Ah so I finally figured it out, as OldCurmudgeon pointed out the issue was the wildcards. 啊,所以我终于明白了,因为OldCurmudgeon指出问题是通配符。 The problem was the compiler didn't know that all of them were going to actually be the same in the end, meaning my Foo class actually had another Type parameter I hadn't thought of, the type of the values used to calculate the end result. 问题是编译器不知道所有这些实际上最终都是相同的,这意味着我的Foo类实际上有另一个我没想到的Type参数,用于计算结束的值的类型结果。

public class Foo<T,I>{

    T val;
    Transformation<List<I>,T> transformation;

    public void setVal(List<I> list>){
        val = transformation.transform(list);
    }
}

public static void main(String[] args){
    Foo<Double,Number> foo = nee Foo(new AveragingTransformer());
    List<Number> nums = new Arraylist<Number>();
    nums.add(2);
    nums.add(3);
    foo.setValue(nums);
}

Thanks for pointing me in the right direction! 谢谢你指点我正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM