如何编写一个在Java中用于替代泛型且向后兼容的类?

huangapple go评论68阅读模式
英文:

How to write class to replace generic in Java that is backwards compatible?

问题

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Data extends ArrayList<Row> implements List<Map<String, String>> {

}

public class Row extends HashMap<String, String> implements Map<String, String> {

    public Integer getInt(String key) {
        return Integer.parseInt(this.get(key));
    }
}
英文:

I have existing code that returns a List<Map<String, String>>.

I would like to create a class that replaces this but will still work for existing code.

I've tried a couple of things but I get a compile error that says either "cannont be implemented more that once" or my Data object cannot be converted to a List<Map<String, String>>.

Searching for answers I find things like the following but not how to do this specifically.

https://stackoverflow.com/questions/7255643/extending-generic-classes

Java Code that gives "cannot be implemented more than once" error.
Data class

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Data extends ArrayList&lt;Row&gt; implements List&lt;Map&lt;String, String&gt;&gt; {

}

Row class

import java.util.HashMap;
import java.util.Map;

public class Row extends HashMap&lt;String, String&gt; implements Map&lt;String, String&gt; {

	public Integer getInt(String key) {
		return Integer.parseInt(this.get(key));
	}
	
}

答案1

得分: 1

我认为在Java中这是不可能的。我认为只有一种机会可以做到这一点,如果您将函数签名更改为接受List&lt;? extends Map&lt;String, String&gt;&gt;而不是List&lt;Map&lt;String, String&gt;&gt;。在这种情况下,这是可能的:

public class Foo {

    public static void main(String[] args) throws ParseException {
        List&lt;Map&lt;String, String&gt;&gt; data1 = Collections.emptyList();
        Data data2 = new Data();

        foo(data1);
        foo(data2);
    }

    public static void foo(List&lt;? extends Map&lt;String, String&gt;&gt; data) {
    }
}

class Row extends AbstractMap&lt;String, String&gt; {

    public Integer getInt(String key) {
        return Integer.parseInt(this.get(key));
    }

    @Override
    public Set&lt;Entry&lt;String, String&gt;&gt; entrySet() {
        return null;
    }
}

class Data extends AbstractList&lt;Row&gt; {

    @Override
    public Row get(int index) {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }
}
英文:

I think this is not possible in Java. Only one chanse to do this I think if you change function signature to accept List&lt;? extends Map&lt;String, String&gt;&gt; instead of List&lt;Map&lt;String, String&gt;&gt;. In this case this is possible:

public class Foo {

    public static void main(String[] args) throws ParseException {
        List&lt;Map&lt;String, String&gt;&gt; data1 = Collections.emptyList();
        Data data2 = new Data();

        foo(data1);
        foo(data2);
    }

    public static void foo(List&lt;? extends Map&lt;String, String&gt;&gt; data) {
    }
}

class Row extends AbstractMap&lt;String, String&gt; {

    public Integer getInt(String key) {
        return Integer.parseInt(this.get(key));
    }

    @Override
    public Set&lt;Entry&lt;String, String&gt;&gt; entrySet() {
        return null;
    }
}

class Data extends AbstractList&lt;Row&gt; {

    @Override
    public Row get(int index) {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }
}

huangapple
  • 本文由 发表于 2020年9月20日 02:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定